Please disregard the last bit - I hit the wrong button!!!!!
This is what I'm trying to say....
Hi Guys,
Not wishing to bother anyone here, I've been struggling with this all
weekend on my own, and got nowhere, so I'm turning to you all for
Here's the problem:1. I have a workbook open that needs to gather data from a series of
other workbooks - one at a time, and each decided by the user.......so
the macro needs to:
a) Let the user browse to the first workbook to be opened
b) Get the filename of that workbook
c) Concatenate two of the columns in that book
d) Define a range for a VLOOKUP in the already open book
e) Create said formula in the already open book
Currently I'm doing it using the following code, which posts the
worbook name in cell B65535 so I can use it as a variable (there's
probably a better way, but I can't think of one). All works fine until
I use that variable in the VLOOKUP formula and it all goes haywire.
I'm not sure if it's a syntax thing - although I've tried all sorsts
of ways - or something else. Here's the code.....
Sub Add_New_Miles()
Dim iLastRow0 As Long
Dim iLastRow1 As Long
Dim iLastRow2 As Long
Dim Bookname As String
FName = Application.GetOpenFilename()
If FName <> False Then
Workbooks.Open Filename:=FName
End If
Range("B65535").Select
ActiveCell.FormulaR1C1 = _
"=MID(CELL(""filename"",R[-65534]C),FIND(""["",CELL(""filename"",R[-65534]C)),FIND(""]"",CELL(""filename"",R[-65534]C))-
FIND(""["",CELL(""filename"",R[-65534]C)))& ""]"""
' This should give a result in the form [filename] to assign to the
variable below....
Bookname = Range("B65535").Value
' The next bit calculates the number of rows in the dataset....
iLastRow0 = Cells(Rows.Count, "A").End(xlUp).Row
' Now it concatenates two columns to use in the lookup later.....
Columns("F:F").Select
Selection.Insert Shift:=xlToRight
Range("F2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
Range("F2").Select
Selection.AutoFill Destination:=Range("F2:F" & iLastRow0),
Type:=xlFillDefault
Range("F2:F" & iLastRow0).Select
' Now a range name ("newmiles") for the lookup is defined...
Range("F2:N" & iLastRow0).Name = "newmiles"
' Next, switch to other book and plant the VLOOKUP formula, which is
meant to look at two values in this spreadsheet and compare them with
the concatenated ones in the other (which works fine when doing this
manually).......This is the bit that's going wrong!!!!!!
Windows("members_cleaned.xls").Activate
Range("AB2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-23]&RC[-22],'" & Bookname &
"'!newmiles,9,FALSE)"
However I word the formula, it comes up with Runtime error 1004 ...
Application- defined or object-defined error. Not being much cop at
this, I haven't a clue what it means..... except that when I practiced
the macro without a variable for the workbook name it was fine. Also,
I've tested it by printing the current variable into a cell an that's
correct....so what could it be?
Sorry the message is verbose - I just wanted to make it as clear as I
can. Can anyone help?????????
Phil