Copy specified columns into new sheet

doriannjeshi

Board Regular
Joined
Apr 5, 2015
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi,
This macro wont copy the entire column if there are gaps in the column with no data, will stop copying the rest of the column.
Can it be fixed to copy the whole column ?

Sheets.Add After:=ActiveSheet
Dim vHeader As Variant, rngFound As Range, i As Long
For Each vHeader In Array("EAN/UPC", "demo", "demo", "demo", "demo demo", "demo", "demo", "demo", "demo demo", "demo demo demo", "demo demo", "demo demo", "demo demo")
Set rngFound = Sheets(1).Cells.Find(vHeader, , xlValues, xlWhole, 1, 1, 0)
i = i + 1
If Not rngFound Is Nothing Then
Range(rngFound, rngFound.End(xlDown)).Copy Destination:=Sheets(2).Cells(1, i)
End If
 

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
Try
VBA Code:
Range(rngFound, Cells(Rows.count, rngFound.Column).End(xlUp)).Copy Sheets(2).Cells(1, i)
Dim vHeader As Variant, rngFound As Range, i As Long
For Each vHeader In Array("demo", "demo", "demo", "demo", "demo")
Set rngFound = Sheets(1).Cells.Find(vHeader, , xlValues, xlWhole, 1, 1, 0)
i = i + 1
If Not rngFound Is Nothing Then

Range(rngFound, Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Sheets(2).Cells(1, i)
 

Attachments

  • 1730653042854.png
    1730653042854.png
    17.4 KB · Views: 4
Upvote 0
What is the name of the sheet the columns are being copied from?
 
Last edited:
Upvote 0
End(xlDown) will stop at the first empty cell. It is normally better to work from the bottom up as Mark showed you.

Maybe explain what you want to do. Quite often showing code that does not work as intended can complicate matters because of wrong thoughts to start with.
 
Upvote 0
as Mark showed you.
I think he is running it with a different sheet active so the Cells need qualifying in the code I posted with the sheet or you get the error shown (I would prefer to use the name as I don't like using it's Index as in the original code)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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