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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello pcal45,

Jim has the part correct on getting the cells on the row.

I want it to loop through until the end of column A and copy and past every one startign at row 16, not just the first line.

I hope this helps clarifies.

Please let me know if you need something further.
 
Upvote 0
I see what is happening.

After 12 columns from Column A, I want it to break and paste on the next line.

I think this will clarify.
 
Upvote 0
Excel Workbook
ABCDEFGHIJKL
1
21234567
32468101214
46121824303642
58468101214
69121824303642
7
8
9
10
11
12
13After Running the attached Macro "concat4"
14
15
1612
1768101214
181824303642
1968101214
201824303642
Sheet1
Excel 2007

Currently, this MAY NOT have the first row (#2) rightly copied, but it does for the remaining ones. COMMENT? Jim


Code:
Sub concat4()
Dim lastrow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
Dim LC As Long, t As Long ', i As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
    LC = Cells(2, Columns.Count).End(xlToLeft).Column
    With Range(Cells(2, 1), Cells(2, 2))
        .Copy Destination:=Cells(16, 1)
    End With
    t = 2
Range("A3").Select
j = 17
Do Until ActiveCell.Address = "$A$" & lastrow + 1
    For i = 4 To LC Step 2
Cells(ActiveCell.Row, i).Copy Destination:=Cells(j, t)
t = t + 1
Next i
ActiveCell.Offset(1).Select
j = j + 1
t = 2
Loop
End Sub
 
Upvote 0
Yes sir, we are definitely almost there.

I also want the info in Column A copied.

It is now stopping on the first month.

You almost have it!

Why is it chopping off the first line now?

FL - Hyster - H50FT 38
7 8 7 8 7 8 4 7 4 7 4 79
6 9 6 9 6 9 4 7 4 7 4 80
20 46 20 46 20 46 24 28 24 28 24 372
1 19 1 19 1 19 4 7 4 7 4 96
9 17 2 17 2 17 5 7 5 7 5 103
7 29 7 29 7 29 16 14 16 14 16 213
3 16 3 16 3 16 9 7 9 7 9 114
 
Upvote 0
Should it (In My Example) HAVE this Completed Look?
Excel Workbook
ABCDEFGHIJKL
1
21234567
32468101214
46121824303642
58468101214
69121824303642
7
8
9
10
11
12
13After Running the attached Macro "concat4"
14
15
1612
1734567
1868101214
191824303642
2068101214
211824303642
Sheet1
Excel 2007
 
Upvote 0
Yes, but with the contents of cell A copied going across 12 months plus the total.
 
Upvote 0
I am LIMITED in what I Can SHOW you on the Screen here do the the limitations of the HTML Maker Software (Can't go past Column M), but the code should work.
Here's my final attempt. Good Luck Jim
Excel Workbook
ABCDEFGHIJKL
1
21234567
32468101214
46121824303642
58468101214
69121824303642
7
8
9
10
11
12
13After Running the attached Macro "concat4"
14
15
1612
17234567
18468101214
19121824303642
20468101214
21121824303642
Sheet1
Excel 2007

Using this Code:

Code:
Sub concat4()
Dim lastrow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
Dim LC As Long, t As Long ', i As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
    LC = Cells(2, Columns.Count).End(xlToLeft).Column
    t = 2
    
    With Range(Cells(2, 1), Cells(2, 2))
        .Copy Destination:=Cells(16, 1)
    End With
Range("A2").Select
j = 17
Do Until ActiveCell.Address = "$A$" & lastrow + 1
    For i = 2 To LC Step 2
Cells(ActiveCell.Row, i).Copy Destination:=Cells(j, t)
t = t + 1
Next i
ActiveCell.Offset(1).Select
j = j + 1
t = 2
Loop
End Sub
 
Upvote 0
I took some more time to understand your code:

I changed the following line

Code:
For i = 1 To LC Step 1

This adds the names in column A but it also copies an extra column.

What can you change to paste the items in column A and just keep the totals? The next column will be dollar amounts. I just want the totals.

Very, very nearly there!
 
Upvote 0
I want it to loop through until the end of column A and copy and past every one startign at row 16, not just the first line.

I hope this helps clarifies.

Please let me know if you need something further.
No it does not! So this is a guess:
Excel Workbook
ABCDEFGHIJKLMNOPQRSTUVWX
1
2A2B2C2D2E2F2G2H2I2J2K2L2M2N2O2P2Q2R2S2T2U2V2W2X2
3A3B3C3D3E3F3G3H3I3J3K3L3M3N3O3P3Q3R3S3T3U3V3W3X3
4A4B4C4D4E4F4G4H4I4J4K4L4M4N4O4P4Q4R4S4T4U4V4W4X4
5
14after running blah:
15
16A2D2F2H2J2L2N2P2R2T2V2X2
17A3D3F3H3J3L3N3P3R3T3V3X3
18A4D4F4H4J4L4N4P4R4T4V4X4
Sheet1


from
Code:
Sub blah()
Set Pattern = Range("A1,D1,F1,H1,J1,L1,N1,P1,R1,T1,V1,X1") '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
Adjust the line:
Set Pattern = Range("A1,D1,F1,H1,J1,L1,N1,P1,R1,T1,V1,X1")
if the columns I've chosen are wrong (highlighted in green in row 2 determined from the result of your original macro running), you can add more, remove some, but retain the refs to row 1 (it's only a pattern of columns).
I have assumed that (1) in post #5 wsa confirmed here, and that (2) was NOT confirmed, but these are only guesses.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
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