Recently, I have a project need to automate a process by combining SSIS package and excel VBA macro into one. Below are the steps:
I have a SSIS package exporting all the view result to multiple individual file from sql server to excel. All the files are saved in same location.
I have one excel VBA macro perform cleaning to remove all the empty sheets in each exported excel file.
I also have a excel VBA macro perform merging task to merge all the excel file into in master excel file. This master excel file contains all the result set and each result set saved on different tabs accordingly.
Since I am manually running step 2 and step 3, so my question is how should connect step 2 and step 3 with step 1 to combine them as one automate process.
Please provide me advice on how likely this can be achieved! Thanks a lot.
The way to do this is to create a script task in your SSIS package.
Then, once inside the script task, you can call the Excel interop through the C# code of the script task. e.g. you can add a reference to Microsoft.Office.Interop. Once you are using that library in your C# code of the script task, you can add some code that will call the macro. e.g.
oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oBooks = oExcel.Workbooks
oBook = oBooks.Open(Dts.Variables("filePath").Value.ToString())
//Your macro here:
oExcel.Run("Yourmacro")
Then you could write code for the other workbooks aswell, for whatever automation you need after this - e.g. you can close the workbook and open another workbook, through the Excel automation as needed.
Related
What I'm trying to accomplish:
I want to upload an Excel table to a SQL Server database table by pushing a button tied to a VBA macro.
I want the macro to wait for the process to complete before continuing.
My current method:
Currently, I have an SSIS package embedded as an object in a cell behind the button tied to the macro. My macro is pretty rough at this stage because I'm still trying to figure out how to run the SSIS package without it opening in the default app.
I want it to run in the background via a shell script. The macro would wait for a signal from the shell script saying that the upload was successful before continuing. I had this running previously with the SSIS package as an external file and ran the shell (which worked); but, I really want it to be contained inside the Excel file.
My code:
Sub UpdateGroupsMacro()
ActiveWorkbook.Save
ActiveSheet.Shapes.Range(Array("Object 1")).Select
Selection.Verb Verb:=Activate
' Shell ("DTEXEC.EXE /F " & ActiveWorkbook.FullName & "\xl\embeddings\oleObject1.bin""") <---This wasn't working even when I went one level deeper.
Application.Wait (Now + TimeValue("00:00:01"))
Worksheets("Macro").ListObjects("GetGroupsTableCount").Refresh
End Sub
I believe my problem is rather simple: I have a workbook and I'm using it to get some data from another software (SAP). When I export the data from the software, it automatically opens a .xlsx file, then what I'd need to do is copy some of the data from this file, paste on my original workbook, and then close the file. The section of my code which is giving me an error is this one:
fileName = "temp1.xlsx"
Set wbBasis = Workbooks(fileName)
This happens because the "temp1.xlsx" file that was opened by the SAP software is in another instance of excel, so my code isn't able to find it.
What I need to know is basically this. How to properly reference this "temp1.xlsx" workbook on my original code so that I'm able to edit it, copy stuff from it, close it, etc.
I've found some similar topics to my problem, like the two I'm listing down here, but couldn't adapt the solutions to my situation, and that's why I'm posting this new one.
Having multiple excel instances launched, how can I get the Application Object for all of them?
Finding a workbook in one of multiple Excel instances
Thank you in advance.
You don't need multiple instances of Excel, and you don't need the Excel file to be open in order to get information from it, either with VBA, or even with regular "linked formulas".
For example:
=SUM([C:\yourPath\Filename.xlsx]SheetName!A1:C25)
...returns the sum of cells A1:C25 on a worksheet named Sheetname in an Excel file located at C:\yourPath\Filename.xlsx.
Or you can import all the data directly with Get External Data.
See:
MSDN : Create an external reference (link) to a cell range in another workbook
MSDN : Connect to another workbook
...for more information and examples, search Google for "get data from another Excel workbook".
I recently started using python 3.6 (trough Anaconda) so I am sorry if the below will result not that clear. The situation is the following:
I have an excel file, which I use to run a macro in order to obtain results from Prophet. My macro works good but, unfortunately, after several times it has been used excel reaches the limit of memory and I cannot extract the results anymore. The way to solve the problem would be to close excel and then open it again.
To do so, in the same vba macro, I open a python script that allows me to run the macro, save the excel file, close it and open it again. The macro is the below:
import win32com.client
object = win32com.client.Dispatch('Excel.Application')
object.visible=1
object.Run("Central_Run")
print("Macro ran successfully!")
print("Active WB:", object.ActiveWorkbook.Name)
for wb in object.Workbooks:
print("WB:",wb.Name)
wb.Save()
#The loop above saves all the workbooks that are open
object.quit()
#Open the Excel file again
object=win32com.client.Dispatch('Excel.Application')
wb=object.Workbooks.open(‘myfile.xlsm')
The script works, however, I would like to open the excel file, in the second part of the script, without specifying its name (wb=object.Workbooks.open(‘myfile.xlsm')), so that I can use the code for other similar excel files with different name. Ideas that I had were either to open the latest used excel file or to link the script to a specific cell in my excel file so that I need just to modify that cell instead of the script, but, unfortunately, I am not able to do it.
Thanks in advance for the help! :)
You can replace your second print statement with this:
wbName=object.ActiveWorkbook.Name
print("Active WB:", wbName)
and then replace the last line with:
wb=object.Workbooks.open(wbName)
I am currently teaching myself VBA. I work with Excel 2010 a lot but only know the basics of VBA.
To help teach myself, I have set a project for myself which is to create a dashboard in Excel where I can click a button and it opens three reports I run each morning at work and runs a macro on each. The process of each report is to
1 - open report
2 - unhide worksheet (worksheet = "Control Sheet")
4 - run macro on the unhidden sheet (macro = "ButtonClick")
5 - hide worksheet
5 - Save and close report
I have managed to get 1 report to open using:
Sub EasierRun()
Dim Location As String
Location = "location/filename.xlsm"
Workbooks.Open(Location).RunAutoMacros (xlAutoOpen)
End Sub
I copied the 2nd to last line from the internet but it doesn't run any macro, it just opens the file. No error messages display. I understand that I need to be specific about the worksheet and macro I want to work with but I'm not sure how to proceed from here. Also, I'm not sure if I need to tell it to unhide the worksheet in the VBA? Finally, would I need to write individual code for each report to open or do I declare the files all at once and then the remainder of the code works universally?
I've googled and read a lot but I can't manage to adapt what I am finding to fit what I need.
Thanks for any guidance.
Unfortunately, for security purposes, you can't force a workbook to enable macros without explicit input from the user.
We receive Excel files daily from our field offices which I have to clean and re-format (about 110 columns and 500 rows-worth) using VBA.
I need to save my VBA as a macro so we can use it to clean up all the workbook we receive by running the macro and saving the edited sheet as a new worksheet by getting the name from UserForm Combobox items.
Where exactly should I store the VBA snippets? I mean when I open the Visual Basic panel, I have these three options:
Running The Code From Microsoft Excel Object :Sheets1(Sheet1)
Running the Code From An Inserted Module
Running the Code From User Form
If I am supposed to use options 1 or 2, how can I call the UserForm for saving the sheet?
I Recomend you to use modules (Option B)
Option C goes with option B, ill explain, you can create a sub in a module in option B, then you can do:
UserForm1.show
In Option B I would writte this code, but before trying this i recomend you to understand a bit more of vba
sub ClearWBs()
'opening workbook
Workbooks.Open Filename:="c:\book1.xls"
'your code
'your code
'below code for saving and closing the workbook
Workbooks("book1.xls").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
end sub
Use Module:
If your VBA code focusses on data summarization and manipulation I suggest you use a Module.(example is what you have described in your question).
Use Form:
If what you wan't to do requires a GUI(Graphical User Interface) then you'll have to resort to Form where you can design your GUI. (example is if you have many fields that the user needs to fill-up with distinct values in which you provide the choices)
Use Excel Object:
If what you wan't to do has something to do with events happening on Worksheet and/or Workbook, like for example whenever you add sheet the macro runs, or when you open or close the workbook the macro runs then you will have to write the macro in the Excel Object.
This is basically what i have in mind, hope this helps.
If you receive files that do not contain VBA and you need to apply the same code on those files all the time then I propose that you either save that code in your personal workbook.
You can see how to do that here: http://office.microsoft.com/en-ca/excel-help/copy-your-macros-to-a-personal-macro-workbook-HA102174076.aspx
This is nice because you can also tie it to keyboard shortcut or just have it always ready for you to use.
The disadvantage is that it will only be set up per user session per computer. What you can do is have that code all set up in a module and then import it into your personal workbook if you change session or if someone else has to do it.
Once it's done, you will not have to include the module in your files your receive again.