While generating hundreds of Office Excel spreadsheets with Office Access is certainly possible, it would be great to add macros to the generated workbooks.
I would like to add the functions to the object "ThisWorkbook" in the VBA project for each spreadsheet on generation. How would one go about doing this?
Thank you in advance!
Under the assumption that the macro's in all generated workbooks are the same,
create a template containing all VBA code (and optionally constant text like headers, footers, print range definitions, etc. - i.e. "everything except data")
create any new workbook from the template
insert your data into the WB object
save as macro enabled worksheet (Excel 2007/2010)
close it
example
Sub CreateWB()
Dim WB As Workbook
Set WB = Workbooks.Add("MacroTemp.xltm") ' contains VBA, ActiveX, etc.
WB.Worksheets("Sheet1").[A1] = "co-cooo!" ' adding data
WB.SaveAs "MyGenWB", xlOpenXMLWorkbookMacroEnabled
WB.Close
End Sub
In Excel 2007/2010 do not forget to save the template as macro enabled template (*.xltm").
Related
Currently, I export data daily from software to excel files.
There's a lot of repetitive tasks so I created a macro.
I open the new exported excel file and then save it as "Macro-Enabled worksheet"
I open the Macro-enabled worksheet
I import the macros into the excel file
I run the macros
Is there a way to run the macro without doing all the steps above using VBS or any other way?
I don't know if there's a solution out there, but I would prefer if an external VBA operator would ask for the location of the exported file and then does the rest
You can easily open any other workbook and run any commands on that workbook. So you can have the following macro in an Excel file MyMacroFile.xlsm and manipulate data in C:\Temp\WorkbookToRunMacroOn.xlsx for example.
Option Explicit
Public Sub DoTasksOnOtherWorkbook()
'open another workbook
Dim OpenWorkbook As Workbook
Set OpenWorkbook = Application.Workbooks.Open(Filename:="C:\Temp\WorkbookToRunMacroOn.xlsx")
OpenWorkbook.Worksheets("Sheet1").Range("A1").Value = "Changed A1 in another workbook"
'don't forget to close the workbook and save or not
OpenWorkbook.Close SaveChanges:=True
End Sub
If you want to ask the user to select a file to open you can use the Application.FileDialog property it returns a file name that you can then use in the Application.Workbooks.Open to open it.
Situation:
I have two workbooks:
Workbook #1: A Downloaded data set from online data repository
Workbook #2: A Master collection of Macros
I have built a collection of Macros to format a data set after it's been downloaded from an online repository. This data set can differ greatly based on features that users have chosen before downloading the data. The macros I created cover all possible scenarios. Thus, when opening the View Macros dialog box there are an overwhelming amount to choose from. Even with efficient naming conventions it's too much to sift through for my audience, who has an average to low experience level with Excel.
In order to simplify their experience, I wanted to level the playing field by simply providing a "Go" button after they choose from an ActiveX dropdown list.
I successfully created the dropdown, populated the list, and upon activation of the "Go" button, the selection triggers a specific Macro to run.
MY PROBLEM:
I need the Subcode on the "Go" button in Workbook #2 to force the macros to run in Workbook #1.
Thank you ahead of time for all your help!
You can take cues from the macro recorder.
Here is some basic code that is stored in one workbook and manipulates cells in another workbook.
Sub Macro2()
Dim mlib As Workbook
Dim wb As Workbook
Dim ms As Worksheet
Dim ws As Worksheet
Set mlib = ActiveWorkbook ' the file with the macros
Set ms = mlib.Worksheets("Sheet1")
' write into the macro workbook
ms.Range("B3").FormulaR1C1 = "asdf"
'activate another workbook that is already open and is called somefile.xlsx
Windows("somefile.xlsx").Activate
' set variables to reference that workbook
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
' write something into somefile.xlsx
ws.Range("B1").FormulaR1C1 = "copy me"
'copy something within somefile.xlsx
ws.Range("B1").Copy ws.Range("B2")
' copy something from the macro workbook to the somefile workbook
ms.Range("A1").Copy ws.Range("A5")
' activate the macro workbook
mlib.Activate
End Sub
I asked for some sample code so I could explain in your context.
There are about a dozen different ways how you can identify the "other" file instead of hard-coding it into the macro. If you could be bothered to provide a bit more information, that little detail could also be taken care of.
So I'm importing data every day into Access to use for reporting. The data comes from several spreadsheets created by different individuals. Because those individuals like to format things incorrectly I created a macro that reformats their document so that it can be imported cleanly into Access for me to use. Works great but it gets tedious having to open up each Excel sheet to run this Macro.
What I'm trying to do is place the Excel Macro in Access and then run the formatting code before importing it all at once. I am a bit lost in approaching this. I'm aware of ways to run Macros already placed in Excel sheets but is there a way to run a macro that is stored in Access that works in excel. I also thought to maybe inject the Macro into the excel document and then run it.
To sum things up, what I'm hoping to do is from Access, store a macro, that can be used to alter Excel Files.
Is this at all possible? If so How? Is there another approach?
What you are asking to do is automate Excel from Access. Yes, you can do this. In Access, add a module, add a reference to the Microsoft Excel object model (Tools: References), and use this framework code to get you started:
Sub PrepExcelFileForImport()
Dim xl As Excel.Application
Dim wbk As Excel.Workbook
Dim wst As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
With xl
.Visible = True
Set wbk = .Workbooks.Open("c:\temp\temp.xlsx")
Set wst = wbk.Worksheets("data")
With wst
' add your formatting code here, be sure to use qualified references, e.g.
.Rows(1).Font.Bold = True
End With
End With
wbk.Close SaveChanges:=True
xl.Quit
End Sub
I have a macro in Outlook, and I have it opening an Excel file that is saved on my desktop. Once the file is open, I would like to run a macro that I have written in excel, but none of my excel macros are available. The macros are available whenever I open excel any other way, and macros are enabled when I open excel through outlook vba. My question is, how do I make these macros available when I open Excel via the Outlook macro? Please reference my code below.
'Pre:None
'Post:Excel will have been opened, and the macro "CreatePowerPoint()"
' will have been run on the excel document
Sub Gimba()
Dim xlApp As Object, xlWkb As Object
'open excel
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True ' can be False if you do not wont see reaction,
' byt make sure is not fail
'Do not show any alerts
xlApp.DisplayAlerts = False
'open excel document
Set xlWkb = xlApp.Workbooks.Open(file path goes here)
'call macro on excel document
Call xlApp.Run("CreatePowerPoint")
End Sub
I assume that the macros that you have normally available are stored in your personal.xls workbook? If so, then you just need to load that up before you try and launch your CreatePowerPoint macro.
Try something like (depends on where your personal workbook is stored):
xlApp.Workbooks.Open ("C:\Documents and Settings\YourUserNameHere\Application Data\Microsoft\Excel\XLSTART\personal.xlsb")
As an aside, you might find it easier to write the VBA code if you use early binding. To do this, you need to add a reference to the Excel object model, then instead of using CreateObject, you could use Set xlApp = new Excel.Application. That way you get all the nice Intellitype assistance.
I am working with an Excel file that was created by somebody else.
One sheet containing Macros appears to be password protected, but what I don't understand is that I cannot see it in VBE under the sheet list. The sheet tab is visible in Excel, but I cannot see the content.
Is there any way to unhide it in VBE?
One sheet containing Macros
Does that refer to Excel 4.0 macros?
Worksheets containing Excel 4.0 macros don't appear to be visible within the list in VBE.
They do appear to be accessible from VBA to some extent: using Excel 2007 I inserted an Excel 4.0 macro sheet to a workbook then tried the following:
Public Sub TestAccessToXL4MacroSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet ' succeeds
Debug.Print ws.Name ' outputs "Macro1"
Set ws = Worksheets("Macro1") ' fails: "Subscript out of range"
End Sub
As far as I know, there is no way you can hide a sheet from VBE! However, you can rename it there (in effect changing the .CodeName of the worksheet). Thus, if you know the Excel worksheet name (the one you see in the Excel worksheet tab), but cannot find it in VBE, go to the Immediate window in VBE (Ctrl-G) and run
? Worksheets("YourName").CodeName - this should give you the name under which it can be found in the VBE Project tree.