Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Running Macros

We use Excel to "dump" information into from our program.
After we are done we need to run a macro while Excel is
still open. Can anyone tell me how to do this from VB
outside of Excel?

Thanks in advance.

Cindy.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Running Macros

Hi Cindy,

We use Excel to "dump" information into from our program.
After we are done we need to run a macro while Excel is
still open. Can anyone tell me how to do this from VB
outside of Excel?


Firstly, if you want to do anything in Excel, you have to do it with Excel. Whether the actual code sits within an Excel workbook or in
VB is up to you - though within the workbook is likely to be quicker. Something like the following should get you started:

1. Add a project reference to the Excel Object library, then use:

Dim oXL As Excel.Application
Dim oDataWB As Excel.Workbook
Dim oCodeWB As Excel.Workbook

'Create a new Excel instance for us to use
Set oXL = New Excel.Application

'Open the data file
Set oDataWB = oXL.Workbooks.Open("Path\YourDataFile.xls")

'Open the workbook containing the macro to run
Set oCodeWB = XL.Workbooks.Open("Path\WorkbookContainingYourCode .xls")

'Run the macro, passing in the name of data workbook,
'so in a standard module in the Code workbook, you'd have
'
'Public Sub RoutineInCodeWorkbook(sDataWBName As String)
'
oXL.Run "RoutineInCodeWorkbook", oDataWB.Name

'Close and save the data workbook
oDataWB.Close True

'Close without saving the macro workbook
oCodeWB.Close False

'Clear down our object variables
Set oDataWB = Nothing
Set oCodeWB = Nothing

'Close Excel and clear down our object reference
oXL.Quit
Set oXL = Nothing


Alternatively, instead of the two lines that open the code workbook and run the routine, you can include all the code to format the
report within the VB module.

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.co.uk


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running 4 macros at once Secret Squirrel Excel Discussion (Misc queries) 2 January 31st 09 09:40 PM
Running Macros Excel Discussion (Misc queries) 1 July 6th 06 04:21 PM
Running Macros Excel Worksheet Functions 0 July 6th 06 03:42 PM
running macros from IF CJ Excel Worksheet Functions 2 April 30th 06 05:03 PM
HELP: Running two Macros, one before the other Bobbak Excel Programming 3 December 8th 03 02:08 PM


All times are GMT +1. The time now is 12:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"