This picture is my excel data format, I wanna export it into text file with fixed column width, example:
User ID Total Work
001 22:00 17:00
002 4:00 4:00
How to set PERSONAL.XLSB auto run my script when always open a newly created specific name workbook?
I tried to add this script into XLSB > ThisWorkBook
Private WithEvents App As Application
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
Application.Run ("PERSONAL.XLSB!TASUBSPayroll")
End Sub
After open the specific name workbook, it was exported a text file without export the data out:
User ID Total Work
I think it was run the macro on PERSONAL.XLSB only and not run to my specific name workbook.
http://i.imgur.com/EETLL6V.jpg
I was add the these code into the module name as "TASUBSMacro" in VBAProject(PERSONAL.XLSB) but it still not run the macro for the TotalTimeCardReport.xlsx.
Private Sub Workbook_Open()
Run "MyMacro"
End Sub
This TotalTimeCardReport.xlsx was exported from some software and auto open it with excel. But I was combined with the PERSONAL.XLSB and it was still not run the macro - module from PERSONAL.XLSB to TotalTimeCardReport.xlsx.
Loop all rows and all cells. Send each value to a padspace function. Build the string from for each cells value with spaces padded after the cell value.
You will have to add a reference to you workbook. In the VBA IDE go to the tools pull down menu and select references. Then scroll down and select "Microsoft Scripting Runtime". Then hit OK.
Adjust the pad space function call argument to a number that fits the data that you have in your spreadsheet. So you will change the 20 in the line with the padspace call. PadSpace(20, len(cellValue))
This will do all rows and columns.
If you only want to target certain rows and columns you can test for values as you loop. And then target columns. Something like this.
Response to user question. To run a macro on workbook open.
"MyMacro" is the name of the procedure in a public module Insert -> Module.
If you want to run code from a private module you cannot use "Run" command just use the procedure name.