Coping data from one sheet to another - matching headers VBA

spattar

New Member
Joined
Aug 7, 2018
Messages
3
Hi,

Can you please helpme with VBA, I have an excel spreadsheet with 2 tabs (data, keyfigure matrix).

I extract data daily of another program on to the keyfigurematrix. And I need VBA code to copy this data over to data tab (withoutdeleting / overwriting previous days), matching the headers which are on toprow on both sheets. However the headers are not in same column every day on keyfigure matrix tab.


Thank you, your help will be much appreciated.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

Can you please helpme with VBA, I have an excel spreadsheet with 2 tabs (data, keyfigure matrix).

I extract data daily of another program on to the keyfigurematrix. And I need VBA code to copy this data over to data tab (withoutdeleting / overwriting previous days), matching the headers which are on toprow on both sheets. However the headers are not in same column every day on keyfigure matrix tab.


Thank you, your help will be much appreciated.
Hi, Please help me. Thanks in advance
 
Upvote 0
Try this macro:
Code:
Public Sub Copy_keyfigure_matrix_To_data()

    Dim dataSheet As Worksheet, dataDestRow As Long
    Dim col As Long, lastRow As Long
    Dim foundCol As Variant
    
    Set dataSheet = ThisWorkbook.Worksheets("data")
    With dataSheet
        dataDestRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    
    With ThisWorkbook.Worksheets("keyfigure matrix")
        For col = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
            foundCol = Application.Match(.Cells(1, col).Value, dataSheet.Rows(1), 0)
            If Not IsError(foundCol) Then
                lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
                .Range(.Cells(2, col), .Cells(lastRow, col)).Copy dataSheet.Cells(dataDestRow, foundCol)
            Else
                MsgBox "Column heading '" & .Cells(1, col).Value & "' not found in data sheet"
            End If
        Next
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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