macro to retrieve data based on column headers

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
680
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm looking for some vba that will retrieve data from a table in one wksht and drop it into a table on another. Looking at the picture below, I would like to be able to enter header values in the cells C5:Q5 in the 'Report' wksht then press the button to execute a macro that will retrieve the data corresponding to the headers in the table in the 'Data' wksht.
Notes:
The Data wksht headers are fixed from A3:AE3
The Data wksht table will vary in size (up to 50,000 rows down)
Not all cells in the Report wksht (C5:Q5) will have entries in them
Any help much appreciated.
Rgds,
b7x7b7.jpg
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
Public Sub CopyDataToReport()

Dim dataSheet As Worksheet
Dim reportSheet As Worksheet
Dim lastRow As Long
Dim thisCol As Long
Dim thatCol As Variant

Set dataSheet = Sheets("Data")
Set reportSheet = Sheets("Report")

With reportSheet
    For thisCol = .Range("C5").Column To .Range("Q5").Column
        lastRow = .Cells(Rows.Count, thisCol).End(xlUp).Row
        If lastRow > 5 Then .Range(.Cells(6, thisCol), .Cells(lastRow, thisCol)).ClearContents
        If .Cells(5, thisCol).Value <> "" Then
            thatCol = Application.Match(.Cells(5, thisCol).Value, dataSheet.Range("A3:AE3"), 0)
            If Not IsError(thatCol) Then
                lastRow = dataSheet.Cells(dataSheet.Rows.Count, thatCol).End(xlUp).Row
                dataSheet.Range(dataSheet.Cells(4, thatCol), dataSheet.Cells(lastRow, thatCol)).Copy Destination:=.Cells(6, thisCol)
            End If
        End If
    Next thisCol
End With

End Sub

WBD
 
Upvote 0
adjust your sheet names accordingly


Code:
'----------
Sub CopyDataOver()
'----------
Dim vHdr
Dim shtTarg As Worksheet, shtSrc As Worksheet
Dim c As Long, iSrcCol As Long
Dim sColLtr As String


Set shtSrc = Sheets("sheet1")   'the source data


Worksheets.Add
Set shtTarg = ActiveSheet   'target data


Sheets("report").Activate          'what headers to copy c5:Q5
Range("C5").Select
While ActiveCell.Value <> ""
   c = c + 1
   vHdr = ActiveCell.Value
   iSrcCol = FindHdr(vHdr)
   sColLtr = Mid(ActiveCell.Address, 2, InStrRev(ActiveCell.Address, "$") - 2)
   
   If iSrcCol > 0 Then
       shtSrc.Activate
       Columns(sColLtr & ":" & sColLtr).Select
       Selection.Copy
       shtTarg.Activate
       Range("A1").Offset(0, c - 1).Select
       ActiveSheet.Paste
   End If
   
   Sheets("report").Activate
   ActiveCell.Offset(0, 1).Select   'next col
Wend
Set shtSrc = Nothing
Set shtTarg = Nothing
End Sub


'----------
Function FindHdr(ByVal pvName)
'----------
On Error GoTo ErrFind
    Sheets("sheet1").Activate
    Rows("3:3").Select
    Selection.Find(What:=pvName, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        
    FindHdr = ActiveCell.Column
Exit Function
ErrFind:
FindHdr = 0
End Function
 
Last edited:
Upvote 0
Awesome!, works perfectly, thanks very much indeed :)
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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