Loop until the end and more eloquent way to write this code

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
I have the following code:

Code:
Sub PivotGraph2()

With Worksheets("Sheet1")
    Range("A2:B2").Copy Range("A16")
    Range("D2").Copy Range("B16")
    Range("F2").Copy Range("C16")
    Range("H2").Copy Range("D16")
    Range("J2").Copy Range("E16")
    Range("L2").Copy Range("F16")
    Range("N2").Copy Range("G16")
    Range("P2").Copy Range("H16")
    Range("R2").Copy Range("J16")
    Range("T2").Copy Range("K16")
    Range("V2").Copy Range("L16")
    Range("X2").Copy Range("M16")
    Range("A3:B3").Copy Range("A16")
    Range("D3").Copy Range("B16")
    Range("F3").Copy Range("C16")
    Range("H3").Copy Range("D16")
    Range("J3").Copy Range("E16")
    Range("L3").Copy Range("F16")
    Range("N3").Copy Range("G16")
    Range("P3").Copy Range("H16")
    Range("R3").Copy Range("J16")
    Range("T3").Copy Range("K16")
    Range("V3").Copy Range("L16")
    Range("X3").Copy Range("M16")
    Range("A4:B4").Copy Range("A16")
    Range("D4").Copy Range("B16")
    Range("F4").Copy Range("C16")
    Range("H4").Copy Range("D16")
    Range("J4").Copy Range("E16")
    Range("L4").Copy Range("F16")
    Range("N4").Copy Range("G16")
    Range("P4").Copy Range("H16")
    Range("R4").Copy Range("J16")
    Range("T4").Copy Range("K16")
    Range("V4").Copy Range("L16")
    Range("X4").Copy Range("M16")
    
      
End With

End Sub

I want it to loop through the end in column A until the end and grab all the months in these ranges associated with Column A. From what Smitty has told me, and I have seen it before is there a way to concantenate all these ranges in one line of code instead of doing it this way?

I am trying to use Excel Jeanie, but I am at work right now, I will try to show the sheet later from my home computer.

If you ened further clarification, please let me know.

Kurt
 
Hello pcal45, and Jim,

Pcal45, I acknowledge you and of course Jim for his efforts:

It works!

Here is my adjusted code:
Code:
Sub blah()
Set Pattern = Range("A2,B2,D2,F2,H2,J2,L2,N2,P2,R2,T2,V2,X2, Z2") 'adjust column letters only
DestRow = 16
LR = Cells(Rows.Count, 1).End(xlUp).Row
For rw = 2 To LR
    Pattern.Offset(rw - 1).Copy Cells(DestRow, 1)
    DestRow = DestRow + 1
Next rw
End Sub

I will still break apart Jim's code as I haev been so I can understand it some more!

I like the sub name blah!

I apologize for frustating you, Jim and anyone else!

Good thing I am not nearby, you might throw beer cans at me or something else!! hehe

Many thanks,

Kurt
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I note that you've changed the row refs of the Pattern range to 2 (I did suggest you didn't) - are you sure you're getting both the the first and last rows of your source data copied over?
 
Upvote 0
Hello P45Cal,

Yes sir it works perfectly!

Many thanks for checking in.

I also like the 50 Cal that can shoot through Cinder Blocks!

Many thanks for your and Jim's help.

I apologize for being a pain!

Best and peace,

Kurt
 
Upvote 0
Well that surprises me, because the code as amended by you copies row 3 first (ignoring row 2) and lastly copies what might be a blank row at the end, 1 row below the value of LR.

p45cal, in this case, is not a gun, just leet for my first name.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top