+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    02-14-2010
    Location
    Australia, Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Refer to a windown whos name is derived from a cell value in a formula

    Any help with this one would be greatly appreciated.

    I want to tell the Index and Match formulas to refer to a window when the window's name is contained in an adjacent cell. The code below should make what I am trying to do clear:

    Code:
    Sub Macro2()
    
    Dim ActWin As Window
    
    ActWin = ActiveCell.Value & ".xlsx"
    ActiveCell.Offset(0, 2).Select
    ActiveCell.FormulaR1C1 = _
     "=INDEX([ActWin]Sheet1!R3C[9]:R12C[9],MATCH(RC2,[ActWin]Sheet1!R3C10:R12C10,0))"
    
    End Sub
    However, I can't define "ActWin" in the way I have tried to above when it is defined as a Window. Changing the first line to "Dim ActWin As String" allows me to set ActWin equal to the value in the active cell however, I am then prompted to locate the file ActWin on my hard drive once the index/ Match formulas are put in.

  2. #2
    Registered User
    Join Date
    02-14-2010
    Location
    Australia, Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Refer to cell value in formula

    Any help with this one would be greatly appreciated.

    I want to tell the Index and Match formulas to refer to a window when the window's name is contained in an adjacent cell. The code below should make what I am trying to do clear:

    Code:
    Sub Macro2()
    
    Dim ActWin As Window
    
    ActWin = ActiveCell.Value & ".xlsx"
    ActiveCell.Offset(0, 2).Select
    ActiveCell.FormulaR1C1 = _
     "=INDEX([ActWin]Sheet1!R3C[9]:R12C[9],MATCH(RC2,[ActWin]Sheet1!R3C10:R12C10,0))"
    
    End Sub
    However, I can't define "ActWin" in the way I have tried to above when it is defined as a Window. Changing the first line to "Dim ActWin As String" allows me to set ActWin equal to the value in the active cell however, I am then prompted to locate the file ActWin on my hard drive once the index/ Match formulas are put in.

  3. #3
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,242

    Re: Refer to cell value in formula

    You may have a misunderstanding of what Window is. When you talk about a Window's name, I think you really mean a Workbook's name. Window does not have a Name attribute. Regardless, this code just works with strings and doesn't really have to know anything about workbooks. I think you want something like this:

    Code:
    Sub Macro2()
    
    Dim ActWin As String
    
    ActWin = ActiveCell.Value & ".xlsx"
    ActiveCell.Offset(0, 2).Select
    ActiveCell.FormulaR1C1 = _
     "=INDEX([" & ActWin & "]Sheet1!R3C[9]:R12C[9],MATCH(RC2,[" & ActWin & "]Sheet1!R3C10:R12C10,0))"
    
    End Sub
    Excel will then try to evaluate the formula and if the file doesn't already exist it requires you to locate it. That is not an issue with VBA, it's just how external workbook references work when using INDEX. The same thing would happen if you just typed in the formula directly into a cell.
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  4. #4
    Registered User
    Join Date
    02-14-2010
    Location
    Australia, Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Refer to cell value in formula

    Thanks a lot.
    Works perfectly.

  5. #5
    Registered User
    Join Date
    03-08-2007
    Posts
    92

    Re: Refer to a windown whos name is derived from a cell value in a formula

    You have the variable name inside the quotation marks, so Excel thinks you want the exact letters ACTWIN inside the formula. You need to separate the text from the variable name as follows.

    Code:
    Sub Macro2()
    
    ActiveCell.FormulaR1C1 = _
     "=INDEX(["&ActWin&"]Sheet1!R3C[9]:R12C[9],MATCH(RC2,["&ActWin&"]Sheet1!R3C10:R12C10,0))"
    
    End Sub
    Hope this helps

    SAE

  6. #6
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,044

    Re: Refer to a windown whos name is derived from a cell value in a formula

    Samnic, I've merged your two identical questions into one thread. In the future please take care not to post the same question twice.

    thanks
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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