开发者

Excel - Using an Address to copy over an entire column including blank spaces

What I'm trying to do is scan over every header on one sheet (the headers are located in "1:1"), find a header called "Dealer Code", then copy that entire column to another sheet.

My current problem is that there are blanks in the Dealer Code column so the selection stops. I would w开发者_运维问答ork from the bottom of the sheet upward to avoid this, but since the header cell is stored in an address, I can't extract the column letter.

There a way I can include blanks in my selection, or extract the column letter and work upwards?

If I'm approaching the problem very inconveniently, please let me know! I want to learn as much as possible.

Below is my code. Help would be greatly appreciated.

'Copies over Dealer Codes
With Sheets("Raw Data").Range("1:1")
Set c = .Find("Dealer Code", LookIn:=xlValues)
If Not c Is Nothing Then
    firstAddress = c.Address
    Do
        Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstAddress
End If

Sheets("Raw Data").Range(firstAddress).Select
Sheets("Raw Data").Range(Selection, Selection.End(xlDown)).Select
Selection.Copy (Sheets("Copied Data").Range("A1"))
End With


Try something like this:

Sub Test()
    With ActiveSheet.Range("1:1")
    Set c = .Find("Dealer Code")
    Dim column As String
    column = Mid(c.Address, 2, 1)
    Range(column & ":" & column).Select
    End With
End Sub

This selects the whole column, but is easily changed to select the elements from the 2nd row down to the end. Since you have blanks, something like Range(column & "2:" & column & Range(column & "65536").End(xlUp).Row).Select works, but is a bit kludgey, I'll admit.

Other solutions may be more optimal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜