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:
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.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
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:
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.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
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:
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.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
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.
Thanks a lot.
Works perfectly.
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.
Hope this helpsCode:Sub Macro2() ActiveCell.FormulaR1C1 = _ "=INDEX(["&ActWin&"]Sheet1!R3C[9]:R12C[9],MATCH(RC2,["&ActWin&"]Sheet1!R3C10:R12C10,0))" End Sub
SAE
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 theicon 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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks