Hi Jack
ok, I put together a sub and i found a sub on google which helps me find
named ranges.
What you have to do is following:
- Put both files in the same folder
- make a button in Target on sheet targetsheet (name can vary, but it needs
to be changed in the vba code)
- copy the button-sub below into the click event of the button
- copy the function below beneath the click event
(i found the function by googling, and just tweaked it a little)
- change the name of the workbook in the VBA code to the one you want
- enter the unique names for your columns starting in B1 and proceeding to
the right
- click the button
Sub for the button
-------------------------------------------------------------------------------------------
Dim awb As Workbook
Dim row As Integer
Dim wb As Workbook
Dim rngStr As String
Set awb = ActiveWorkbook
row = awb.Worksheets("targetsheet").Cells(65536, 1).End(xlUp).row + 1
'set date in first column of last row
awb.Worksheets("targetsheet").Cells(row, 1).Value = Format(Now(),
"yyyy-mm-dd")
a = ActiveWorkbook.Path & "\source.xls" 'change the name of the workbook
Set wb = Workbooks.Open(a)
awb.Activate
With awb.Worksheets("targetsheet")
For i = 2 To .Cells(1, 256).End(xlToLeft).Column
rngStr = NamedRangeExists(.Cells(1, i).Value, wb.Name)
If rngStr <> "" Then
.Cells(row, i).Value = rngStr
End If
Next i
End With
wb.Close
Set wb = Nothing
End Sub
-------------------------------------------------------------------------------------------
function to copy
-------------------------------------------------------------------------------------------
Function NamedRangeExists(strName As String, _
Optional wbName As String) As String
'Declare variables
Dim rngTest As Range, i As Long
'Set workbook name if not set in function, as default/activebook
If wbName = vbNullString Then wbName = ActiveWorkbook.Name
With Workbooks(wbName)
On Error Resume Next
'Loop through all sheets in workbook. In VBA, you MUST specify
' the worksheet name which the named range is found on. Using
' Named Ranges in worksheet functions DO work across sheets
' without explicit reference.
For i = 1 To .Sheets.Count Step 1
'Try to set our variable as the named range.
Set rngTest = .Sheets(i).Range(strName)
'If there is no error then the name exists.
If Err = 0 Then
'Set the function to TRUE & exit
NamedRangeExists = rngTest
Exit Function
Else
'Clear the error
Err.Clear
End If
Next i
End With
End Function