+ Reply to Thread
Results 1 to 3 of 3

FormulaR1C1 to refer to workbook from text string

Hybrid 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.

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

    Re: FormulaR1C1 to refer to workbook from text string

    If i attempted to change the last line of code to this:

    ActiveCell.FormulaR1C1 = "='[" & targetfile & "]Summary'!R2C3"
    then the link put into the cell shows up as
    ='[C:\Users\Alkuan\Documents\[Example.xlsm]Summary]Example.xlsm]Summ'!R2C3

    I'm really quite confused about how the syntax of this statement is effecting the outcome.

    Here is a different forum post i found that is somewhat related

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

    Re: FormulaR1C1 to refer to workbook from text string

    I found a very clumsy workaround that does work, but there has to be a better way.

    
    Dim targetfile
    Dim tempbook As Workbook
    Dim databook As Workbook
    
    Set databook = Workbooks("Database.xlsm")
    
    targetfile = Application.GetOpenFilename("Excel Files (*.xlsm),*.xlsm")
        If Filename <> "False" Then
            Set tempbook = Workbooks.Open(targetfile)
        End If
        
    databook.Activate
    
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Database")
    
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
        
    Worksheets("Calculation").Activate
    Range("A1") = targetfile
    targetfile = Range("A2")
    
    ws.Activate
    Cells(iRow, 1).Select
    ' Enters the link into the appropriate cell
    ActiveCell.FormulaR1C1 = "='[" & targetfile & "]Summary'!R2C3"
    Where the cell A2 in calculation has the following formula in place.

    =RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"\","~",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))
    This returns the part of the string after the last '\'. Which in this instance is the file name. By inputting this into the cell when the referenced workbook is open, it automatically is converted into the full reference when the workbook is closed at the end of the code.

    For example, if this string is input into A1. C:\Users\Alkuan\Documents\Example.xlsm
    A2 returns, Example.xlsm

    As i said, very clumsy and there must be a better way to achieve this.
    Last edited by Alkuan; 05-30-2012 at 10:11 AM.

+ Reply to Thread

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