Change end row reference VBA

jufglanville

New Member
Joined
Sep 11, 2017
Messages
23
Hi all, I'm very new to VBA but came across a piece of code that does pretty much exactly what I want but am wanting to modify it slightly but have no idea how. Basically the code looks at a heading from one sheet and looks to see if it exists in another and if it does copies the column contents and places it back on the first sheet.

My problem is that some of the cells in the columns contain blanks and I am using the xlDown function so all data past this point is missed. I am wanting to change this 'xlDown' to the last row number found in column A as this column never contains missing cells.

Below is the code:

Code:
Private Sub CopyHeaders()    Dim header As Range, headers As Range
    Set headers = Worksheets("FPS").Range("A1:BK1") '


    
    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("TempTable").Cells(2, GetHeaderColumn(header.Value))
        End If
    Next
End Sub


Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("TempTable").Range("A1:Y1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function

Any help would be great, thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try (untested)...

Code:
Range(header.Offset(1, 0), Cells(Rows.count, header.Column).End(xlUp)).Copy
 
Upvote 0
Try
Code:
Private Sub CopyHeaders()
    Dim header As Range, headers As Range
    Dim Usdrws As Long, x As Long
    Usdrws = Worksheets("FPS").Range("A" & Rows.Count).End(xlUp).Row
    Set headers = Worksheets("FPS").Range("A1:BK1")


    
    For Each header In headers
        x = GetHeaderColumn(header.Value)
        If x > 0 Then
            Range(header.Offset(1, 0), header.Offset(Usdrws)).copy Destination:=Worksheets("TempTable").Cells(2, x)
        End If
    Next
End Sub


Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("TempTable").Range("A1:Y1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function
This also cuts out the second function call on each column
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

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