Open Excel with batch file

M

MrsSoto

Hi, I have created a batch file to open an excel workbook that I use so that
I can run it as a scheduled task. It looks like this:
start "C:\WINDOWS\Desktop\Business.xls"

Is there a command or switch that I can use at the end to make it open to a
particular worksheet instead of the worksheet that was open when last saved?

Thank you very much for any help.
 
E

Earl Kiosterud

MrsSoto,

In your batch file, you could open a workbook which contains a macro that
opens your workbook, switches to the desired sheet, and then even closes
itself. Something like the following, in module ThisWorkbook:

Private Sub Workbook_Open() ' Excel runs this automatically
Workbooks.Open ("Book1.xls")
Workbooks("Book1.xls").Sheets("Sheet2").Select
ThisWorkbook.Close
End Sub

It's barebones. For example, it doesn't check that book1 was found, or that
there was a sheet2 to select. It's meant to run in what we call an
"error-free" environment. That means that nothing will go wrong Insert
laugh audio here.

Tip: Since it closes itself, if you need to open it and work on the macro,
hold Shift as you open it. that keeps the macro from running automatically.
Or disable macros if you have security set such that you get the macro
warning message.
 
M

MrsSoto

Thank you very much, Earl It's a little over my head so I'll have to study
it for a little while and attempt to get it to work. I never ran a macro in
Excel ( or anywhere else ).
 
E

Earl Kiosterud

MrsSoto,

Alt-F11 gets you to the Visual Basic Environment (VBE). The Project
Explorer will show your workbook (looks like an outline). Double-click
module "ThisWorkbook" to open a window for it (right pane). Paste in the
code. Modify the names in the code as needed. Put the cursor anywhere in
the module, press F5 (run). Switch back to Excel (Alt-Tab, etc.). See what
it did. Be sure to save the file containing the workbook.
 
E

Earl Kiosterud

MrsSoto,

Well, THAT one may have confused a few folks. I meant to say "Be sure to
save the file containing the macro." Not workbook.
 
M

MrsSoto

Thanks for the advice, I will try it as soon as I get some time and some
courage. Thanks again.
 
Top