![]() |
running a macro saved as a string
I have an Access app that manipulates a spreadsheet. The spreadsheet
can be in one of several pre-defined formats. For each, i have recorded in Excel a macro that performs the necessary re-formatting. I have stored these as strings in a table. I want to: 1) based on user choice, create a spreadsheet in one of the possible formats, 2) open it and then play the appropriate macro My VBA code (in Access) looks like this: Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = New Excel.Application Set xlBook = xlApp.Workbooks.Open( ... Name of spreadsheet ... ) Now, if the text of the macro is in a string variable (call it strMacro), what do i call in the Excel object model to execute it ? |
running a macro saved as a string
Try
Eval(xlApp.PublicSub) -----Original Message----- I have an Access app that manipulates a spreadsheet. The spreadsheet can be in one of several pre-defined formats. For each, i have recorded in Excel a macro that performs the necessary re- formatting. I have stored these as strings in a table. I want to: 1) based on user choice, create a spreadsheet in one of the possible formats, 2) open it and then play the appropriate macro My VBA code (in Access) looks like this: Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = New Excel.Application Set xlBook = xlApp.Workbooks.Open( ... Name of spreadsheet ... ) Now, if the text of the macro is in a string variable (call it strMacro), what do i call in the Excel object model to execute it ? . |
Not the issue: running a macro saved as a string
No -- not the issue.
I have the macro as a saved string -- example: Rows("1:1").Select Selection.Font.Bold = True Range("C1").Select ActiveCell.FormulaR1C1 = "'Status" Range("D1").Select ActiveCell.FormulaR1C1 = "'Activity #" Range("B1,F1,G1,H1,I1,J1,K1,L1").Select Range("L1").Activate Selection.HorizontalAlignment = xlRight This string is stored in a variable in the Access app. It is NOT in some sub in the spreadsheet. How do i pass it to Excel for execution? -=-=-= KM wrote: Try Eval(xlApp.PublicSub) -----Original Message----- I have an Access app that manipulates a spreadsheet. The spreadsheet can be in one of several pre-defined formats. For each, i have recorded in Excel a macro that performs the necessary re- formatting. I have stored these as strings in a table. I want to: 1) based on user choice, create a spreadsheet in one of the possible formats, 2) open it and then play the appropriate macro My VBA code (in Access) looks like this: Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = New Excel.Application Set xlBook = xlApp.Workbooks.Open( ... Name of spreadsheet ... ) Now, if the text of the macro is in a string variable (call it strMacro), what do i call in the Excel object model to execute it ? . |
Not the issue: running a macro saved as a string
http://www.cpearson.com/excel/vbe.htm
-- Regards, Tom Ogilvy "jfp" wrote in message ... No -- not the issue. I have the macro as a saved string -- example: Rows("1:1").Select Selection.Font.Bold = True Range("C1").Select ActiveCell.FormulaR1C1 = "'Status" Range("D1").Select ActiveCell.FormulaR1C1 = "'Activity #" Range("B1,F1,G1,H1,I1,J1,K1,L1").Select Range("L1").Activate Selection.HorizontalAlignment = xlRight This string is stored in a variable in the Access app. It is NOT in some sub in the spreadsheet. How do i pass it to Excel for execution? -=-=-= KM wrote: Try Eval(xlApp.PublicSub) -----Original Message----- I have an Access app that manipulates a spreadsheet. The spreadsheet can be in one of several pre-defined formats. For each, i have recorded in Excel a macro that performs the necessary re- formatting. I have stored these as strings in a table. I want to: 1) based on user choice, create a spreadsheet in one of the possible formats, 2) open it and then play the appropriate macro My VBA code (in Access) looks like this: Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = New Excel.Application Set xlBook = xlApp.Workbooks.Open( ... Name of spreadsheet ... ) Now, if the text of the macro is in a string variable (call it strMacro), what do i call in the Excel object model to execute it ? . |
Not the issue: running a macro saved as a string
Tom Ogilvy wrote:
http://www.cpearson.com/excel/vbe.htm OK -- thanks. I skimmed through this; i need to create a module in the spreadsheet, paste the saved macro string into it, and then run the macro. Is there a shorter/simpler way? This is a "one-time" macro -- it reformats certain cells / column widths etc. to make the spreadsheet more legible than that created by Access. There is no need to save it in the spreadsheet. Seems like the ability to build a string containing some commands and then execute it directly would be a nice feature in a language this powerful ... |
Not the issue: running a macro saved as a string
The evaluate command cited earlier is supported in VBscript I believe, but
not VBA. I don't know how robust it is in terms of executing your strings. Personally, if you already have the commands written out, put them in subs in Access and use a case statement to execute the appropriate sub. I suspect the capability doesn't exist because there appeared to be little need for it. -- Regards, Tom Ogilvy "jfp" wrote in message ... Tom Ogilvy wrote: http://www.cpearson.com/excel/vbe.htm OK -- thanks. I skimmed through this; i need to create a module in the spreadsheet, paste the saved macro string into it, and then run the macro. Is there a shorter/simpler way? This is a "one-time" macro -- it reformats certain cells / column widths etc. to make the spreadsheet more legible than that created by Access. There is no need to save it in the spreadsheet. Seems like the ability to build a string containing some commands and then execute it directly would be a nice feature in a language this powerful ... |
All times are GMT +1. The time now is 12:14 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com