Currently, I have a sub which downloads an Excel file from the Internet. I would like to open the spreadsheet and run more commands on it prior to the sub ending.
The issue is that the spreadsheet does not open until the macro has finished executing thus it cannot find the workbook.
With appIE
.Navigate "https://www.alerian.com/wp-content/uploads/AMZmembers.xls"
.Visible = False
Application.Wait Now + TimeValue("00:00:03")
SendKeys "{Enter}"
End With
Application.Workbooks("AMZmembers.xls").Close
SendKeys "{Enter}" opens the sheet but it will not open until the sub has finished; therefore, Workbooks.close cannot find the sheet because it isn't open yet.
Appreciate help!
Use Workbooks.Open "path_and_filename" instead of the SendKeys. There are a bunch of other parameters in the Open method such as whether you want it opened read-only, etc but the simplest way is as shown.
Related
I am using Microsoft Task Scheduler to run Excel models everyday. The models retrieve data from the database and do stuff. The connection strings contain all information(username, password etc.) needed to connect to the database. However, I get this prompt sometimes:
SQL Login Prompt
When I click "OK", everything goes fine, so I need the Excel to just click OK because the username and password are already there. I want to suppress this window by clicking OK. Otherwise the models stop running and wait for me to click OK manually.
I checked for the solutions on the web but did not encounter a working one. I tried to change the connection string in various ways but it did not work.
Does anyone have any idea on how to solve this issue?
Thanks
Edit: Here is the code I am running on VBScript for Task Scheduler.
'Input Excel File's Full Path
ExcelFilePath = "MyExcelFile"
'Input Module/Macro name within the Excel File
MacroPath = "Macro1"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Input Module/Macro name within the Excel File
MacroPath = "Macro2"
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
The excel VBA code for Macro1 is just
Sub UpdateModel()
ActiveWorkbook.RefreshAll
End Sub
command for pivot tables which are getting data from the database.
I realize this question has been asked before, but the supplied answers did not solve the problem. I'm running VBA code from within Excel which does the following:
1.) Open a local worksheet (i.e. C:\desktop\AOR\Names.xlsx)
2.) Read down column B getting the names of files that need to be verified
3.) Open the filename (as taken from column B) from a Network drive.
4.) Verify some cells from the opened file
5.) Close the file.
6.) Iterate.
All this is working with no problems; my situation is that when the file is opened from the Network drive (i.e. step 3) I get the "Downloading File" message. The only option on this is "Cancel" or let it go to completion. Pressing cancel (or using SendKeys to do so) causes the file open to cancel, thus stopping the rest of the process. I've had users accidentally press this cancel enough times that I want to suppress the dialog box completely but cannot find anything that works.
Here is some code with the options I've tried all to no avail:
WhatName = Workbooks(AgencyNamesList).Worksheets("Agencies").Range("B" & i).Value ' get name of file from locally opened workbook.
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.EnableEvents = False ' Prevent code in workbook from running
' Application.SendKeys "{ESCAPE}", False ' Cancels subsequent file opens
' Application.SendKeys "{ESCAPE}", True ' Cancels the file open
' Application.SendKeys "~", True ' Cancels the file open
QuarterDueDate = Workbooks.Open(SourceFolder & WhatName).Worksheets("YTD").Range("I6").Value ' open book get value of I6
This page shows a picture of the dialog box along with someone else trying to find a solution (since 2012) to this problem.
OPSYS: Windows 7 Enterprise SP1
EXCEL: MS Office Professional Plus 2013
I'm sure hoping someone has found a way to prevent this pop-up box from popping!
NOTE: When this code is run against the same files stored locally, the dialog box does NOT display.
Thanks!
I have an excel workbook that I download using simple HTML portal that I coded. When I download the workbook it automatically renames it as "myWorkbook (27)" or whatever number of times it has been put in the downloads folder.
I have some code that changes the Save As location of the workbook to the original location that it was downloaded from. See:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
'Check to see if another workbook with the same name is open -- close if so.
If IsOpenWorkbook("myWorkbook.xlsm", True) Then
Workbooks("myWorkbook.xlsm").Close saveChanges = False
End If
ThisWorkbook.SaveAs Filename:="\\999.9.9.99\folder\anotherFolder\myWorkbook.xlsm"
Application.DisplayAlerts = True
End Sub
This has worked fine for almost a month but recently has been performing erratically. When I first download the sheet, it has the regular "Enable Editing" prompt which I accept. After accepting, the name of the workbook changes suggesting the code as run, but then has another "Enable Editing" prompt which, if I accept, will crash Excel.
Any insights appreciated.
I am automating testcases for my company's addin.
In one of the testcases, after saving my workbook on my local machine when I try closing the workbook, I am required to click on 'Yes'- saves edited data in our database, 'No'- discards the edits or 'cancel'- simply closes the message box.
I have tried using sendKeys but that didn't work for me.
Eg: For clicking 'No' through my VBA code, I tried to send tab and enter keys like this:
Application.Wait (Now() + TimeValue("00:00:10"))
Call SendKeys("{TAB}", True)
Call SendKeys("{ENTER}", True)
I have tried with various time values still didn't get any success. I would be glad if someone can help me with this.
Thanks in advance! :)
UPDATE: As of now, I have been doing this:
'Some tasks...
ActiveWorkbook.Save
Call Application.Run("workbook.xlsm!Discard")
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
But is there any way to get the window object reference for the message box? Or anyway to stick to the workflow: edit workbook -> save -> try closing -> opt to discard/commit changes to database?
NOTE: By discarding/committing changes to database, what I mean is a server where the data gets updated. Excel is just an interface for the user. I don't want just the excel to be saved but the operation of calling discard/save changes on server as well...
if you have already saved the workbook, you only need to close the workbook. Please try the below code
Activeworkbook.close false
This will close your workbook and you wont be promted for selecting any options.
I would like to test an Excel VBA app I made.
However the VBA code messes around with the visibility of cells and that's a pest when editing the sheet.
Is there an option is enable and disable macro's on the fly without having to
Close the sheet
Change the macro settings
Reopen the sheet
Close the sheet
Change the macro settings.
etc.
As far as I know, you can't enable / disable macros from an opened workbook on the fly.
Yet, you shouldn't have to because macros are only triggered thanks to a user click.
The only case I would see is for the Event Procedures (Worksheet_Change or else).
You could then create procedures to activate / deactivate events and call them from buttons in your worksbook:
Sub enableEvents()
Application.EnableEvents = True
End Sub
Sub disableEvents()
Application.EnableEvents = False
End Sub
You can also try these tips from Chris Pearson website using global vars you would change depending on your needs:
Public AbortChangeEvent As Boolean
And check if afterwards:
Private Sub Worksheet_Change(ByVal Target As Range)
If AbortChangeEvent = True Then
Exit Sub
End If
'
' rest of code here
'
End Sub
To disable macros on the fly, use "Application.EnableEvents = False" via the Immediate window in the VBA editor (and "Application.EnableEvents = True" to turn them back on).
You can also hold down SHIFT when you open a document to disable macros.
As of Excel 2010* there's actually an option on the "Developer" tab which allows you to disable macros as easy as ABC. Design Mode lets you do just that!
*Maybe even earlier versions.
I often fire Macros when opening a workbook but sometimes I don't want the Macro to fire so I can work on the code. So what I put a Macro in a separate workbook that does the following:
Disables macros
Opens the workbook in question
Enables macros
Then closes the original workbook
Leaves the second workbook open
Here's the code:
Sub OpenClose()
'Opens Workbook below with Macors disabled
'After it is open Macros are enabled
'This Workbook then closes without saving changes, leaving only the workbook below open
'************************************************************
'User only needs to change the workbook name on the next line
WorkbookToOpenNoMacros = "Gaby.xlsm"
'************************************************************
Dim wb As Workbook
Set wb = Application.ThisWorkbook
Application.EnableEvents = False
Application.Workbooks.Open (ActiveWorkbook.Path & "\" & WorkbookToOpenNoMacros)
Application.EnableEvents = True
wb.Saved = True
wb.Close SaveChanges:=False
End Sub
I have Office 2013 and I found a button on the VBA window that did this for me very easily.
There's a play, pause and stop button on the toolbar. In Excel, they are actually called, Run Macro, Break and Reset.
Click the Break button (pause) and any running macros should stop running. I only tested it on one macro but seems reasonable to presume that this will work in general.
Also, I believe those buttons were there for many versions of Excel, so it's worth checking earlier versions.