Copy specified columns into new sheet

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
301
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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try
VBA Code:
Range(rngFound, Cells(Rows.count, rngFound.Column).End(xlUp)).Copy Sheets(2).Cells(1, i)
 
Upvote 0
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
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)
the name is sheet1
 
Upvote 0
VBA Code:
Range(rngFound, Sheets("Sheet1").Cells(Rows.count, rngFound.Column).End(xlUp)).Copy Sheets(2).Cells(1, i)

Obviously assumes that Sheets(1) is the same sheet as Sheets("Sheet1") as rngFound needs to be the same sheet
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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