So i've got a little bit of a tricky problem here, i thought i had the right code but its coming out a little odd when i execute it, and i can't figure out why.
I'm attempting to create a macro which will open a prompt to select a workbook using the GetOpenFilename command, so far, so good. I then want to use the FormulaR1C1 command to refer to a specific cell (C2) within the targeted workbook. This is the code i'm currently using:
Sub AddNew()
Dim targetfile
Dim databook As Workbook
Set databook = Workbooks("Database.xlsm")
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database")
targetfile = Application.GetOpenFilename("Excel Files (*.xlsm),*.xlsm")
databook.Activate
'Finds the next open row
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Cells(iRow, 1).Select
ActiveCell.FormulaR1C1 = "=' " & targetfile & "Summary'!R2C3"
End Sub
The last line of code is where i am running into trouble.
When i run the macro, the following string shows up in the next empty row.
='C:\Users\Alkuan\Documents\[Example.xlsmSummary]Example'!R2C3
The correct reference should be :
='C:\Users\Alkuan\Documents\[Example.xlsm]Summary'!R2C3
if i display the string 'targetfile' right before this step, it shows up as
C:\Users\Alkuan\Documents\Example.xlsm
Any guidance as to where i've gone wrong would be greatly appreciated!
Thanks in advance.
Bookmarks