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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Your current codes looks like it is Copying Row 2 to row 16 then
Copying Row 2 OVER your previously pasted row 16 then
Copying Row 3 OVER your previously pasted row 16...(*&$()*$#@@

Am I reading this right?
 
Upvote 0
Correction, (Should have read)...

Your current codes looks like it is Copying Row 2 to row 16 then
Copying Row 3 OVER your previously pasted row 16 then
Copying Row 4 OVER your previously pasted row 16...(*&$()*$#@@
 
Upvote 0
Last edited:
Upvote 0
Could you answer jim may's qustion. In addition, below shows the result of your macro on a sheet where I've put cell addresses as data in the cell range you're copying from, and highlighted in green the pattern of cells used in each row. Could you confirm two things:
1. That the source pattern is exactly as shown and not intended to be a regular alternate columns pattern.
2. That the destination range misses out column I as shown.
Excel Workbook
ABCDEFGHIJKLMNOPQRSTUVWX
1
2A2B2C2D2E2F2G2H2I2J2K2L2M2N2O2P2Q2R2S2T2U2V2W2X2
3A3B3C3D3E3F3G3H3I3J3K3L3M3N3O3P3Q3R3S3T3U3V3W3X3
4A4B4C4D4E4F4G4H4I4J4K4L4M4N4O4P4Q4R4S4T4U4V4W4X4
5
14
15
16A4D4F4H4J4L4N4P4R4T4V4X4
Sheet1
 
Upvote 0
It appears in your original code that some cells which are being Pasted (with the copy) ARE IMMEDIATELY Being Re-Pasted (Overwritten), by a LATER PASTE..., But Anyway, try something like this on A COPY (not your Original file). Stepping thru the code (Using the F8 key might be helpfule to you..

In a STANDARD MODULE:

Code:
Sub concat()
Dim LC As Long, t As Long, i As Long
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
For i = 4 To LC Step 2
Cells(2, i).Copy Destination:=Cells(16, t)
t = t + 1
Next i
End Sub
 
Upvote 0
Many thanks for your efforts!

We are almost there.

Instead of copying only row 16, I need it to copy all the way down column A. In this case there are 9 rows, but these will vary.

Almost there!
 
Upvote 0
I am trying to add this lastrow.

Can someone show me what I amd doing wrong?

Code:
Sub concat()
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("A2").Row.Cells(Rows.Count, "A").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
    For i = 4 To LC Step 2
Cells(2, i).Copy Destination:=Cells(16, t)
t = t + 1
Next i
End Sub

I think this lastrow will get to where I want this to be.
 
Upvote 0
I'm still UNCERTAIN as to some thngs you are doing (as once pated cells get overwritten), but here is the Latest - It strings your copied cells continually OUT to the Right as many
columns as are necessary. TRY THIS ON A BACKUP COPY OF YOUR FILE!!! Jim

Code:
Sub concat3()
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
Do Until ActiveCell.Address = "$A$" & lastrow + 1
    For i = 4 To LC Step 2
Cells(ActiveCell.Row, i).Copy Destination:=Cells(16, t)
t = t + 1
Next i
ActiveCell.Offset(1).Select
Loop
End Sub
 
Upvote 0
There are 8 items in column A.

I want it to loop in column A to the end and use your copy and paste routine.

I hope this helps clarify.

This still only copies one line of code.

Thanks for your effort!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

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