+ Reply to Thread
Results 1 to 4 of 4

Variable reference in macro formula

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2008
    Location
    Brisbane, Australia
    Posts
    2

    Variable reference in macro formula

    Range("B7").Select
    ActiveCell.FormulaR1C1 = "=[454679845456.xls]Promoforma!R6C5"

    This is my current formula in VB where I am trying to create a formula in one file linking to another. In the above, the fomula is extracting info from the 454679845456.xls file into a summary book. Is there a way that I can have the "454679845456.xls" portion of the formula as a variable? Possibly pulling from whatever other file is open at the time as it won't always be called 454679845456.xls.

    I hope this makes sense.

    At the moment i have a macro set-up where one file is 'saved as' based on a number inputed in one box. in the above, they have have inputted the number 454679845456. the next step i am trying to achieve is to have this number and the data on this new sheet exported to a summary page. at the moment my formula will keep pulling from the 454679845456.xls file as i don't know how to have this as a variable; pulling in the data from the current open book might be an option but if they more than one file open then this may not work.

    appreciate any assistance you can give.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Welcome to the forum.
        Dim s       As String
    
        s = "454679845456.xls"
        Range("B7").FormulaR1C1 = "=[" & s & "]Promoforma!R6C5"
    Please read the Forum Rules and then edit your post to wrap your code with Code Tags.

  3. #3
    Registered User
    Join Date
    08-27-2008
    Location
    Brisbane, Australia
    Posts
    2
    Quote Originally Posted by shg View Post
    Welcome to the forum.
        Dim s       As String
    
        s = "454679845456.xls"
        Range("B7").FormulaR1C1 = "=[" & s & "]Promoforma!R6C5"
    Sorry, not sure I get this. Will this allow me to change the S value depending what is in a specific cell? Have run this and it always seems to reference to the 454679845456.xls file. What I need is S to be variable and change depending on what is in cell C7. Would just use an index formula but this doesn't work when the other file is closed.

    Maybe I can change s = to a range. is that possible? Sorry for the noob questions.

    Please help... Cheers

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    The example just demonstrates that you can use a variable. You can assign the value of s from the contents of a cell in some open workbook,
    s = Workbooks("myWorkbook.xls").Worksheets("Sheet1").Range("A1").Value
    ...or any other way you like.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro for autofilling formula
    By Gus80 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2008, 06:32 PM
  2. VB Formula error in macro, How to Continue?
    By Mikeco in forum Excel General
    Replies: 7
    Last Post: 04-30-2008, 07:29 PM
  3. Replies: 1
    Last Post: 10-31-2006, 08:34 AM
  4. Entering variable data into a formula
    By Althas in forum Excel General
    Replies: 8
    Last Post: 10-30-2006, 05:15 PM
  5. Macro clears contents and my formula references
    By moike in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2006, 01:04 PM

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