I created an an Excel macro that works correctly however when I add this code to my macro in PERSONAL.XLS from the start directory of MS Excel (in a bad place in the macro), I can't open the macro and I can't find it..
How can I open the macro to delete this bad code!!!
Dim sKill As String
sKill ="TASKKILL /f /IM EXCEL.EXE"
....
shell sKill , vbHide
... Why not just remove your personal.xls from that directory and start over??
OR Remove it, rename it as a.xls or anything else and then open it.
OR set your Tools > macro options so that the macros don't run.
Related
I have a VB Script file that I open using a task in Windows Task Scheduler each morning. The script opens MS Excel and then runs a macro stored in a workbook. The problem is that the macro runs differently if I use the VB Script file to kick off the macro versus when i run it myself from the workbook. Specifically, I find that "Refresh All" doesn't actually refresh all data connections when I run the macro starting from the VB Script file. It works fine if I run the macro from the workbook itself. I'm thinking that I am missing something in this VB Script to ensure MS Excel opens correctly and makes "refresh all" work properly. Anything plainly wrong with this VB Script?
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
ObjExcel.Application.Visible = True
Set ObjWB = ObjExcel.Workbooks.Open("K:\Analytic Reporting\11.Projects\TonyAdHoc\Autorefresher\DashboardAutorefresher.xlsm")
objExcel.Application.Run "DashboardAutorefresher.xlsm!Main"
ObjWB.Close True
ObjExcel.Quit
Set ObjExcel = Nothing
It seems that you are trying to do a refresh to pull data from a database, but you are not seeing any new data from this script when you manually open the Excel file after the script runs.
You will need to save the Excel file after making modifications.
ObjExcel.Save
For anyone still caring about this, what I've found is that for connections to a database table, if the property "Enable Background Refresh" is selected, then the vba command in RefreshAll in the marco doesn't work. So I add a little loop to disable that property for all the connections just to make sure before I call RefreshAll. That fixed it for me.
' -- make sure enable background refresh is not checked else RefreshAll doesn't work
Dim cn As WorkbookConnection
For Each cn In ActiveWorkbook.Connections
cn.OLEDBConnection.BackgroundQuery = False
Next cn
' -- then ok to refresh all
ActiveWorkbook.RefreshAll
I recently started using python 3.6 (trough Anaconda) so I am sorry if the below will result not that clear. The situation is the following:
I have an excel file, which I use to run a macro in order to obtain results from Prophet. My macro works good but, unfortunately, after several times it has been used excel reaches the limit of memory and I cannot extract the results anymore. The way to solve the problem would be to close excel and then open it again.
To do so, in the same vba macro, I open a python script that allows me to run the macro, save the excel file, close it and open it again. The macro is the below:
import win32com.client
object = win32com.client.Dispatch('Excel.Application')
object.visible=1
object.Run("Central_Run")
print("Macro ran successfully!")
print("Active WB:", object.ActiveWorkbook.Name)
for wb in object.Workbooks:
print("WB:",wb.Name)
wb.Save()
#The loop above saves all the workbooks that are open
object.quit()
#Open the Excel file again
object=win32com.client.Dispatch('Excel.Application')
wb=object.Workbooks.open(‘myfile.xlsm')
The script works, however, I would like to open the excel file, in the second part of the script, without specifying its name (wb=object.Workbooks.open(‘myfile.xlsm')), so that I can use the code for other similar excel files with different name. Ideas that I had were either to open the latest used excel file or to link the script to a specific cell in my excel file so that I need just to modify that cell instead of the script, but, unfortunately, I am not able to do it.
Thanks in advance for the help! :)
You can replace your second print statement with this:
wbName=object.ActiveWorkbook.Name
print("Active WB:", wbName)
and then replace the last line with:
wb=object.Workbooks.open(wbName)
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
I am trying to open a file in VBA in Excel 2010 and the code looks like this:
Workbooks.Open Filename:=Folder & FileName
When I execute the code in Excel 2010, it gives me this error:
Can't execute the code in break mode
The file I am opening is of format Excel-2003 format. Is it because of this? I have searched in Google and most suggest it is because of Select statement but I am not using one.
Need some guidance on this.
Workbooks.open runs the workbook_open macro in the workbook it opens. If macros are not enabled for the new workbook, it stops.
I have a program in an Excel workbook that opens a Powerpoint-File, updates the links within this file and closes it after that. This works fine. Here is my problem: When the links are updated an Excel file with the source data is opened. After Powerpoint is closed this file stays open. I want it to get closed because I repeat this process for many files and I can't end up with hundreds of open Excel files.
I tried the following:
WBKs=Application.Workbooks.count
For i = WBKs to 1 Step -1
If Workbooks(i).Name<>ThisWorkbook.Name then
Workbooks(i).close savechanges:=False
End if
Next i
Now comes the weird part. Whenever I just run my code, WBKs always returns 1 and the Excel file only pops up after the code is finished. If I go through my code in debug mode it works. The workbook pops up as soon as I enter debug mode.
I tried Applicatio.Wait in the hope that the file would show after a second. The file only showed after the code was finished.
I tried a Do While Loop to wait until the file is open. Excel crashes because I never leave the loop.
Edit: I tried DoEvents as suggested. Does not work either.
This is just a workaround, but try using a brute force after x times your macro has run. Store that x somewhere in workbook, save. And kill excel process (all instances, including self) :
Dim sKill As String
sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide
Found here : VBA script to close every instance of Excel except itself
When running your macro next time, you will use that x as a starting point for next PPT file to update.