Results 1 to 3 of 3

FormulaR1C1 to refer to workbook from text string

Threaded View

  1. #1
    Registered User
    Join Date
    02-08-2012
    Location
    Gloucester, MA
    MS-Off Ver
    Excel 2010
    Posts
    10

    FormulaR1C1 to refer to workbook from text string

    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.
    Last edited by Alkuan; 05-29-2012 at 05:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1