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.
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,956
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try (untested)...

Code:
Range(header.Offset(1, 0), Cells(Rows.count, header.Column).End(xlUp)).Copy
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,237
Office Version
  1. 365
Platform
  1. Windows
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,237
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,126,965
Messages
5,621,860
Members
415,862
Latest member
nascaline

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
Top