+ Reply to Thread
Results 1 to 5 of 5

Replace filename with variable in "Activecell.FormulaR1C1=" expression

Hybrid View

pommyb Replace filename with... 02-27-2009, 05:07 AM
DonkeyOte Re: Replace filename with... 02-27-2009, 06:09 AM
pommyb Re: Replace filename with... 03-04-2009, 02:17 AM
Tomkat Re: Replace filename with... 11-05-2010, 03:49 PM
daddylonglegs Re: Replace filename with... 11-05-2010, 04:04 PM
  1. #1
    Registered User
    Join Date
    02-27-2009
    Location
    Perth, W.Australia
    MS-Off Ver
    W. Australia
    Posts
    2

    Replace filename with variable in "Activecell.FormulaR1C1=" expression

    I am new to Excel VBA so excuse me if I am asking a silly question. I have recorded a macro which contains several lines of code like this one:

    ActiveCell.FormulaR1C1 = "='[OCR 14-02-09.xls]sun (2)'!R38C4:R38C5"
    I would like to replace the filename "OCR 14-02-09.xls" with a variable. I have tried several different combos of brackets and quotes but always get an error message. Is this possible?

    Thanks
    pommyb
    Last edited by pommyb; 03-04-2009 at 02:13 AM. Reason: Solved

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Replace filename with variable in "Activecell.FormulaR1C1=" expression

    pommyb,

    first please edit your last post and adjust your closing CODE tag to [*/CODE*] without asterisks

    to then answer your Q

    Dim strFile As String
    strFile = "OCT 14-02-09.xls"
    ActiveCell.FormulaR1C1 = "='[" & strFile & "]sun (2)'!R38C4:R38C5"
    Though this formula doesn't make much sense given you're referencing a 2 column range D38:E38 ... you should be looking to "do" something with it... SUM / COUNT etc...

  3. #3
    Registered User
    Join Date
    02-27-2009
    Location
    Perth, W.Australia
    MS-Off Ver
    W. Australia
    Posts
    2

    Re: Replace filename with variable in "Activecell.FormulaR1C1=" expression

    Donkeyote,

    Thanks, your solution worked just fine. With regard to your last comment, I recorded that part of the macro.

    Regards

    pommyb

  4. #4
    Registered User
    Join Date
    03-03-2009
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Replace filename with variable in "Activecell.FormulaR1C1=" expression

    Does anyone know why THIS DOESN'T work?

    Sub testst()
    Dim strFile As String
    
    strFile = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm")
    
    ActiveCell.FormulaR1C1 = "=COUNTA('[ &strFile& ](CV-Summary)'!$A:$A)"
    
    End Sub
    I just want the filename (closed file) that it counts cells from to be a variable... why is this so difficult?

  5. #5
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,698

    Re: Replace filename with variable in "Activecell.FormulaR1C1=" expression

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.
    Audere est facere

+ 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