Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA copy column of non-contiguous data

Status
Not open for further replies.

Hcleslie

MIS
Feb 27, 2005
11
US
What is the best way to select and copy a column of non-contiguous data without copying the first row that contains a column heading?

Thanks
 

Hi,

Please describe and example of your column of non-contiguous data.

What row is column heading in?
What row does data start in?
How many empty rows?
How many total rows?

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
The column heading will be in row 1.
The data will start in row 2, but might be blank.
The number of empty rows will vary. The data source was not originally a required field.

Total rows should be less than 5,000.

Thank you
 
Code:
Sub NameColumn(iCol As Integer)
    Range(Cells(1, iCol), _
        Cells(Cells(Cells.Rows.Count, iCol).End(xlUp).Row, iCol)).CreateNames _
            Top:=True, Left:=False, Bottom:=False, Right:=False
End Sub

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Range(cells(2,1),cells(cells(65536,1).end(xlup).row,1)).copy

- assumes column to copy is "A"

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 

[blush]Geoff, where was my head? COPY![blush]

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Skip - I was wondering but I thought that there would be some method in the madness......obviously your head was by the pool, supping a margherita !!

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 

What? You think I'm a parrot-head???

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
well - a banana daquiri then ;-)

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Skip and Geoff,

Thank you both for the information and the humor.

Skip, I am not able to run you code, when I paste this into the VBE and try to run, it doesn't process or recognize this as a macro. I understand that I need to finish this up with the copy command, but I must be leaving out a step to have this recognized as a macro?

Geoff, your code looks very clean and runs very fast. As you mentioned in your response, it only picks first column. I have changed the numbers in hopes I could modify this to pick other columns as well, but I was only able to have it select column one and adjacent columns. Could you please explain logic and structure so I could modify this to run on other columns?

Thank you,

Charlie


 


You have to pass the column number like this...
Code:
   CopyColumn 2
for instance for column B -- or if you'ld rather use letters...
Code:
   CopyColumn Cells(1,"B").column
Code:
Sub CopyColumn(iCol As Integer)
    Range(Cells(1, iCol), _
        Cells(Cells(Cells.Rows.Count, iCol).End(xlUp).Copy
End Sub


Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
In terms of mine, you would simply change the "1"s ie to get it to work on column 2 (or "B"), this:

Range(cells(2,1),cells(cells(65536,1).end(xlup).row,1)).copy

would become

Range(cells(2,2),cells(cells(65536,2).end(xlup).row,2)).copy

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top