What is the command in Excel VBA to delete a specific macro? - vba

I have an Excel Macro Template. I run a macro on it and it saves the new spreadsheet as an xlsx instead.
I'm just wondering if there's a simple command for VBA that just deletes a macro by name.
I've researched this a lot, and there are basically two answers which don't really fit.
The first is to not have macros in the workbook I'm saving and to just run macros from one workbook to another. (I don't want to do this for a few reasons, but simplicity is the main reason.)
The second is a VBA script that strips ALL VBA and connections from the workbook. (I don't need it to do that much, and I'd rather just delete the one macro I have.)
So, what I'm looking for is just something like this:
Delete.Macro("Import") 'This command deletes the macro in this workbook named "Import".

try
Sub main()
Application.DisplayAlerts = False
Workbooks("PutWorkBookName").SaveAs FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub

Fun thought, use VBA to remove VBA... but seems more work than necessary.
What about activeworkbook.saveas "filename.xlsx", xlExcel12?
As I am sure you are aware, an .xlsx doesn't have any code, so if you set warnings to false, etc., it will just save the file and delete the code.
Now, if I misread, and you want to delete only some of your code, leave the rest, and save as xlsx... you are out of luck.

Related

How to keep unrelated workbooks open in excel when running a macro

I am writing a macro in VB in excel 2013. The only remaining bug left is when the macro runs, all of the other workbooks i have open close, even ones unrelated to the macro. I do not want users of my macro to lose other tabs of excel they may have open if they run my program. I have tried using "Application.Visible = true" but it is not giving me the desired behavior. Is there a way to run a macro without affecting any other currently open workbooks? Or is it possible that something else I have written is overriding the Application.Visible method? If someone could provide a recommendation it would be much appreciated.
Application.Visible = false
Simply does what it says, set the application "Excel in your case" to invisible.
If you want to hide something specific you have to call this:
e.g Sheet
Sheets("Whatever").Visible = False
e.g Workbook
ActiveWorkbook.Windows(1).Visible = False
//ActiveWorkbook needs to have an reference to the specified Workbook which you get as an example if you store the opening of the workbook (ActiveWorkbook = Workbooks.Open )
It sounds like you have a line
Workbooks.close
(which will close all workbooks) when what you wanted to do was close the specific workbook with the macro. That should be
ThisWorkbook.Close

Run Macro from Desktop Shortcut

I wrote a macro that does some calculations based on a particular type of excel sheet. Im trying to distribute this to my coworkers but the addition of a macro to a workbook and then running the macro is something foreign to them. I'd like to have a "shortcut" or some VBS program to open a specific workbook (specified by the user), run the macro, and display the results.
Your help is appreciated!
--Edit--
I wrote a macro in VBA. I exported the file to my desktop. Its simply called "Macro1". We have a standard form of excel sheet our company uses. Its literally the same sheet with different numbers. The macro I designed works on these kinds of sheets and does calculations. My coworkers aren't good with macros, so I want some sort of "code" that will prompt one of my coworkers for an excel file, then execute the macro on the file. Hopefully this clarifies any questions.
You need to make it a excel add-in.
Then in the add-in make it run on workbook open with Sub App_SheetActivate(ByVal Sh As Object) in thisworkbook.
In the macro you can then have it only activate on certain workbook name or workbook type by:
If range("A1").value = "something" ' something that makes the workbook type special.
' Maybe you need B1 value and so on too.
Do you need a way to self-install the add in just let me know and I have a code for that too.
Self install:
' if add-in is not installed and the workbook is a add-in (workbookcount =0)
' Also take note that this code will only run if the add-in is not installed
If Dir(Application.UserLibraryPath & "YourWorkbookName.xlam") = "" And Workbooks.Count = 0 Then
'optional ask user if he wants to install or not. Code not included.
' copy file from current position to add-ins folder.
Result = apiCopyFile(ThisWorkbook.FullName, Application.UserLibraryPath & "YourWorkbookName.xlam", False)
' activate the add-in
AddIns("YourAdd-inName").Installed = True
msgbox("add-in installed")
' Close Excel since add-ins does not work without restart of Excel
On Error Resume Next
Application.Interactive = False
AppActivate "Microsoft Excel"
Application.Quit
Exit Sub
End If
Note that the file must be saved as a add-in. (xlam) this means there is no sheets, the workbook is VBA code only.
Normally, that does not mean the code needs to be written in a special way.
Usually Range("XX").value works, but some commands may need to point towards the correct workbook. (you have two workbooks open with add-ins, the add-in with the code and the workbook with the sheets and numbers)
Hope this helps

VBA error 1004 on Paste from CSV file with CSV file opened first

I'm a beginner on Excel VBA and working on a little tool at work. We use a tool to measure our NPS-score, and therefore I can download all our comments as CSV file. I created a VBA, to paste all these comments in an worksheet and convert it to columns and do the math.
If I open my Excel file first, then download the comments, manually copy the comments and run the script, it works fine. But if I download the comments at first, then open my Excel file and run the script, it throws a 1004 error, Method Paste of class Worksheet failed.
I've tried a lot of answers on here, but none of them seems to work. This is the part of script in the beginning to paste the comments:
Application.ScreenUpdating = False
Dim wb As Workbook
Set wb = ThisWorkbook
wb.Activate
Sheets("Hulpblad").Visible = -1
Sheets("Hulpblad").Paste Destination:=Sheets("Hulpblad").Range("A1")
Application.CutCopyMode = False
I also tried to record a macro and use the Excel generated code, but all the answers give the same error.
Next to that, by some users of the script it doesn't matter in which order you open the files, it always throws the 1004 error. Can somebody please help :-)
1004 is an error, that is generated, when you try to change a protected sheet.
In your code, you are trying to paste a value, which cannot be done, because the sheet is protected.
Try to unprotect it first, before pasting the value and then protect it again.
I think there is no copy method in your statement. You are trying to paste the content directly

Where Exactly To Store VBA

We receive Excel files daily from our field offices which I have to clean and re-format (about 110 columns and 500 rows-worth) using VBA.
I need to save my VBA as a macro so we can use it to clean up all the workbook we receive by running the macro and saving the edited sheet as a new worksheet by getting the name from UserForm Combobox items.
Where exactly should I store the VBA snippets? I mean when I open the Visual Basic panel, I have these three options:
Running The Code From Microsoft Excel Object :Sheets1(Sheet1)
Running the Code From An Inserted Module
Running the Code From User Form
If I am supposed to use options 1 or 2, how can I call the UserForm for saving the sheet?
I Recomend you to use modules (Option B)
Option C goes with option B, ill explain, you can create a sub in a module in option B, then you can do:
UserForm1.show
In Option B I would writte this code, but before trying this i recomend you to understand a bit more of vba
sub ClearWBs()
'opening workbook
Workbooks.Open Filename:="c:\book1.xls"
'your code
'your code
'below code for saving and closing the workbook
Workbooks("book1.xls").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
end sub
Use Module:
If your VBA code focusses on data summarization and manipulation I suggest you use a Module.(example is what you have described in your question).
Use Form:
If what you wan't to do requires a GUI(Graphical User Interface) then you'll have to resort to Form where you can design your GUI. (example is if you have many fields that the user needs to fill-up with distinct values in which you provide the choices)
Use Excel Object:
If what you wan't to do has something to do with events happening on Worksheet and/or Workbook, like for example whenever you add sheet the macro runs, or when you open or close the workbook the macro runs then you will have to write the macro in the Excel Object.
This is basically what i have in mind, hope this helps.
If you receive files that do not contain VBA and you need to apply the same code on those files all the time then I propose that you either save that code in your personal workbook.
You can see how to do that here: http://office.microsoft.com/en-ca/excel-help/copy-your-macros-to-a-personal-macro-workbook-HA102174076.aspx
This is nice because you can also tie it to keyboard shortcut or just have it always ready for you to use.
The disadvantage is that it will only be set up per user session per computer. What you can do is have that code all set up in a module and then import it into your personal workbook if you change session or if someone else has to do it.
Once it's done, you will not have to include the module in your files your receive again.

VBA Do not prompt to Save Macro

I have a VBA macro that makes prompts the user to save the changes to the Personal Macro Workbook each time it is run. Is there any way to disable this message?
I have tried this: ThisWorkBook.close SaveChanges:=Falsebut it still prompts the message
If there are no changes made to the Personal Macro Workbook, or if you don't want changes to be made, just use ThisWorkbook.Saved = True. You can also try setting Application.DisplayAlerts to False, call a close to the workbook, and turn the DisplayAlerts to True again.
The above can be seen here.
If you have formulas in a worksheet in the personal macro workbook, Excel will automatically recalculate them every time the workbook is opened and this constitutes a change which Excel will ask you to save.
Get round it by not putting formulas in the worksheets in the personal macro workbook. If your macro requires formulas, you could always create these in another workbook which your macro can access when it runs.