Jeff Meyers
Active Member
- Joined
- Mar 14, 2012
- Messages
- 405
Using Excel 2010 on a Windows 7 PC<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
I am creating numerous Excel files for employee timesheets. Hours are tracked by day by program grant for bi-weekly pay periods. Each employee will have a separate Excel file containing 27 worksheets (1 master and 26 - one for each pay period). I will create the “Master” to properly allocate each employee’s time to the various program grants. I then need to copy the hours information from the “Master” to each of the pay period worksheets. I named ranges (both on the Master and on each of the pay period sheets) for this process.<o></o>
<o></o>
I created (recorded) a macro to accomplish this, but it is inefficient and I was looking for a way to accomplish the same thing with a For-Next loop. Here is what works:<o></o>
<o>
</o>
<o></o>
I understand that I can have a For-Next loop step through this process 26 times using an array of the range names, but I’m unsure on the syntax. In addition, I’d rather not have to type each of the range names in VBA.<o></o>
<o></o>
I tried experimenting with using a For-Next loop and having VBA reference a list of the range names contained in one of the worksheets, but I was unsuccessful. Here is what I came up with to test it with 5 of the worksheets (this does not work):<o></o>
<o></o>
<o>
</o>
<o></o>
The main issue (I believe) is that my reference is not valid.<o></o>
<o></o>
Any and all help / guidance / direction would be much appreciated. Thanks!<o></o>
<o></o>
I am creating numerous Excel files for employee timesheets. Hours are tracked by day by program grant for bi-weekly pay periods. Each employee will have a separate Excel file containing 27 worksheets (1 master and 26 - one for each pay period). I will create the “Master” to properly allocate each employee’s time to the various program grants. I then need to copy the hours information from the “Master” to each of the pay period worksheets. I named ranges (both on the Master and on each of the pay period sheets) for this process.<o></o>
<o></o>
I created (recorded) a macro to accomplish this, but it is inefficient and I was looking for a way to accomplish the same thing with a For-Next loop. Here is what works:<o></o>
<o>
Rich (BB code):
</o:p>
Application.Goto Reference:="HBPGMaster"<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Application.Goto Reference:=”HBPG15”<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
Range("A1").Select<o:p></o:p>
<o:p></o:p>
Application.Goto Reference:=”HBPG16”<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
Range("A1").Select<o:p></o:p>
<o:p></o:p>
Application.Goto Reference:=”HBPG17”<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
Range("A1").Select<o:p></o:p>
<o:p></o:p>
Application.Goto Reference:=”HBPG18”<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
Range("A1").Select<o:p></o:p>
<o:p>
<o></o>
I understand that I can have a For-Next loop step through this process 26 times using an array of the range names, but I’m unsure on the syntax. In addition, I’d rather not have to type each of the range names in VBA.<o></o>
<o></o>
I tried experimenting with using a For-Next loop and having VBA reference a list of the range names contained in one of the worksheets, but I was unsuccessful. Here is what I came up with to test it with 5 of the worksheets (this does not work):<o></o>
<o></o>
<o>
Rich (BB code):
</o:p>
' Declare variables<o:p></o:p>
Dim c As Variant<o:p></o:p>
Dim tsr As Range<o:p></o:p>
Set tsr = Range("U51:U55")<o:p></o:p>
<o:p></o:p>
' Copies the hours by program grant from Master worksheet<o:p></o:p>
Application.Goto Reference:="HBPGMaster"<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
' Pastes the hours by program grant to each timesheet<o:p></o:p>
' Also de-selects the paste range on each worksheet<o:p></o:p>
For Each c In tsr<o:p></o:p>
Application.Goto Reference:=(c)<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
Range("A1").Select<o:p></o:p>
Next c<o:p></o:p>
<o:p>
<o></o>
The main issue (I believe) is that my reference is not valid.<o></o>
<o></o>
Any and all help / guidance / direction would be much appreciated. Thanks!<o></o>