VBA Help - Change my code to fit new requirements

Kestral79

New Member
Joined
Jul 27, 2015
Messages
2
Hi,

i have the code below that moves data from one work sheet to another.

This code works as follows

  • Iterate across each column header in <CODE>Sheets Summary</CODE> and see if a matching header exists in <CODE>Sheets Log </CODE>
  • <CODE></CODE>If a match is found, copy the column contents across to the relevant column in <CODE>Sheets Log. </CODE>

i am trying to adapt the code to paste the data on the next avalible row & to referance a workbook rather than a sheet.

can anyone help ?
Code:
Sub CopyHeaders()
    Dim header As Range, headers As Range
    Set headers = Worksheets("ws1").Range("A1:Z1")

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

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

thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Kestral79,
I understand your code, but don't understand your question, what is it you are trying to achieve?
Maybe something like this helps:
Code:
Set actwb = ActiveWorkbook
Set wb = Workbooks("your_opened_file.xlsx")
Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=wb.Worksheets("ws2").Cells(2, GetHeaderColumn(header.Value))
Cheers,
Koen
 
Upvote 0
can you add this with past especial option because i am using but some formulas in ws1 not coming to ws2 with paste special. its shown as #NA
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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