Discussion Locked

A more advanced macro for a beginner

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Okay. I am not all that familiar with ins and outs of VB code, but for people that are, my question probably is not all that complicated. If I were to have say 100 files in a folder, and I needed to do the same thing in each folder (so I create a macro with a shortcut key), rather than opening each file and running that macro 100 times, how would I write up a macro that tells it to open each file in folder (x), run my macro, then save and close and continue with every file within the folder until finished?
Then, for the more complicated question. Say I have a master report in Excel (came from an SQL statement ran through an HRIS database) that contains info. such as every employee's supervisor's name, their review date, salary etc. . .Now I want to run a macro that will take this master report and create a bunch of individualized reports; either in another Excel spreadsheet or into a template I created in Word. Basically, a supervisor report, giving each supervisor all of the info. of the employees that report directly into that person. For a huge report this could be a pretty tedious task of repeated cut and paste. (For various reasons, I am currently unable to create this report customized from our HRIS database into Access until I can obtain the ODE tools) So how could I create a macro that will tell it to, if Supervisor =(x) then take that info. and place in (y). etc. . . All info. in the same format, taken from the same report, but individualized into many personalized reports through a macro. Any suggestions?

5 replies

by csanosi

I am not sure about the answer to your first question. I am sure that it is possible, but I have never personally done it. As for the second answer, it is actually quite easy. Without knowing more details about what you want to do, my advice is to record a macro selecting the data you want and paste it into a newly created workbook. Then edit that macro and put it in a loop so that it will process the entire file for you. Finally, when you have your macro working correctly put the following line of code in, Application.ScreenUpdating = False . It will prevent the screen from updating and will make your macro run much faster. If you would like to provide some details, I could get you started with the actual code for the macro.

by jposner42

I know that I need to create a loop in the macro to have it run through the whole file, but I don't know how to do that. Do I jsut write the code or record a macro on the first section of a copy, open a template workbook, paste info., save file name as that of the supervisor and then edit that loop through everyone. How do I do that second part?

by csanosi

Okay here goes. First create a new worksheet for each supervisor (Sheets.Add). I would name each sheet the value that would be in the supervisor field. Then sort the file by the supervisor field ( Selection.Sort Key1:=Range("supervisor field"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom).
Finally define three variables, probably as longs, if you report is more than 32,000 rows. One will be the current row and one will be the last row you can get this with a function like this
Function GetLastRow() As Long
GetLastRow = ActiveCell.SpecialCells(xlLastCell).Row
End Function
. Now loop through all the rows with a for loop.
For lngCurrRow = 1 to lngLastRowcheck the supervisor field. if it has changed, then set lngNewSuper = lngCurrRow. Once it changes again, copy the range of rows from lngNewSuper = lngCurrRow - 1 to the corresponding worksheet and set lngNewSuper = lngCurrRow again.
Rows(lngNewSuper & ":" & lngCurrRow).Select
Selection.Copy
Sheets("supervisor value").Select
Range("A1").Select
ActiveSheet.Paste

Next

Let me know if this works for you. If not, I will send you a sample worksheet with the macros included and you can modify them from there. Good Luck

Chris

by jposner42

it makes sense to me but I am afraid that I do not know enough to be able to set it up myself. If you could send me that sample worksheet that I could step into and modify it would be greatly appreciated. Thanks for your help. Sorry I did not reply sooner, but I have just moved to Japan from New York so I have not had a chance to check this.

This discussion is closed to new replies.