How to copy Non-contiguous cells into a another sheet in VBA

johnston

New Member
Joined
Mar 14, 2018
Messages
49
I have a big table that from row 5 to row 4934, and goes from columns A to O. I created a macro to create a new worksheet with three columns to copy the data over from the other worksheet. Values from columns A,D,G,J and M in the original worksheet need to go in the first column of my new worksheet. The problem is that I need to the data to be copied row by row. For example, if cell A5=245 and cell D5=16, I would need 245 to be inserted first into my first column on my new worksheet and 16 right under it in the same column.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I want to make sure I understand completely what you want.
1. Copy Cell values in columns A, D, G, J, and M in rows 5 through 4934.
2. Paste the values in Column A of the new sheet transposed. This would put the data in A1:A24645.
3. Please confirm that this is what you wish to happen or explain clearly if this is not what you want.
 
Upvote 0
I want to make sure I understand completely what you want.
1. Copy Cell values in columns A, D, G, J, and M in rows 5 through 4934.
2. Paste the values in Column A of the new sheet transposed. This would put the data in A1:A24645.
3. Please confirm that this is what you wish to happen or explain clearly if this is not what you want.

Exactly
 
Upvote 0
How about
Code:
Sub CopyTrans()
   Dim Cl As Range, Ary
   For Each Cl In Sheets("[COLOR=#ff0000]Pcode[/COLOR]").Range("A5:A4934")
   Ary = Array(Cl.Value, Cl.Offset(, 3).Value, Cl.Offset(, 6).Value, Cl.Offset(, 9).Value, Cl.Offset(, 12).Value)
      Sheets("[COLOR=#ff0000]end[/COLOR]").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(5).Value = Application.Transpose(Ary)
   Next Cl
End Sub
Change sheet names in red to suit
 
Upvote 0
Maybe this loop

Code:
Option Explicit


Sub Johnston()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Dim lr As Long, i As Long, lr2 As Long
    lr = s1.Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    For i = 5 To lr
        lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
        Application.Union(Range("A" & i), Range("D" & i), Range("G" & i), Range("J" & i), Range("M" & i)).Copy
        s2.Range("A" & lr2 + 1).PasteSpecial xlPasteValues, , , True
    Next i
    Application.CutCopyMode = False
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    MsgBox "Action Completed"
End Sub
 
Upvote 0
How about
Code:
Sub CopyTrans()
   Dim Cl As Range, Ary
   For Each Cl In Sheets("[COLOR=#ff0000]Pcode[/COLOR]").Range("A5:A4934")
   Ary = Array(Cl.Value, Cl.Offset(, 3).Value, Cl.Offset(, 6).Value, Cl.Offset(, 9).Value, Cl.Offset(, 12).Value)
      Sheets("[COLOR=#ff0000]end[/COLOR]").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(5).Value = Application.Transpose(Ary)
   Next Cl
End Sub
Change sheet names in red to suit

Works great. Would I just change the offset numbers to do this for the other two columns that I need?
 
Upvote 0
If you need further columns, then simply add them to the Ary and change the resize from 5 to however many columns you're copying.
However if I've got the wrong columns, then yes just change the offsets to suit.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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