Output of formula as cells for Excel's Indirect() - vba

I'm using the INDIRECT() function to dynamically refer to worksheets. It seems to me that the function requires a known cell address as input of the formula in quotation marks. However, since I don't know the cell address in advance, I would like to input a cell address that is an output of another formula (e.g., VLOOKUP() or INDEX(MATCH()) ). Is there a way to do this?

Yes, you can compose the string required by INDIRECT() by concatenating formulas that return values that look like a cell address or parts of a cell address.
For example, if your Vlookup or your Index/Match formula returns "A1", then you can use that like
=Indirect(Vlookup(this,there,2,false))
Or, if one formula returns "A" and the other formula returns "1", then combine the output of the two formulas with the & sign.
=Indirect(Vlookup(columnLookup,Table,2,false)&Vlookup(RowLookup,Table,2,false))

Related

VBA Excel - Update Cell value based on other cell value

I am trying to write a formula in a Range of cells based on other range cells values from another worksheet. The formula is shown below:
ActiveSheet.Range("G14:G43").Formula = "=Worksheets("1ºperíodo").Range("V14:V43").Value"
But the code doesn't work and I get a syntax error. That must be related with the strings but I don't know how to fix it.
The value at V14 must be equal to the value at G14 until the last cell, i.e., the value at G14 equals the value at V43. Besides the syntax error is the formula correct based on what I expect to have?
"=Worksheets("1ºperíodo").Range("V14:V43").Value"
is not a formula. It is a value.
If you only want the static values then just assign the values:
ActiveSheet.Range("G14:G43").Value = Worksheets("1ºperíodo").Range("V14:V43").Value
If you want a live formula you need to pull vba from the string and use the .Address function:
ActiveSheet.Range("G14:G43").Formula = "=" & Worksheets("1ºperíodo").Range("V14").Address(0,0,,1)
But the above can be simplified to:
ActiveSheet.Range("G14:G43").Formula = "='1ºperíodo'!V14"
With the formula, we only need to refer to the first cell with a relative reference and vba will make the changes to each row.

INDEX & MATCH instead VLOOKUP

I have 2 validation lists, both have numbers & texts amongst them (but none of the lists have alpha-numeric symbols). They are on another sheet called "Source".
I'm using the statement of:
=INDEX(Sources!$A$3:$A$32,MATCH(LEFT(Sources!$A$3:$A$32,3)&"*",Sources!$B$3:$B$42,0)).
But I keep getting #N/A. Can anyone help?
You will not be able to lookup a range within another range using MATCH formula. In your formula you are using match to lookup A3:A32 within B3:B42. Ideally the match formula can lookup only 1 value in B3:B42 and not the complete range of values.
Your formula should be,
=INDEX(Sources!$A$3:$A$32,MATCH(LEFT(Sources!$A$3,3)&"*",Sources!$B$3:$B$42,0))
If you want to drag formula varying the A3 to A32 , use the below,
=INDEX(Sources!$A$3:$A$32,MATCH(LEFT(Sources!A3,3)&"*",Sources!$B$3:$B$42,0))
This type of lookup with range within a range works only on array formulas. Your formula seems much of array formula. After pasting the formula in formula bar, press ctrl + shift + enter . This will make your formula as an array formula. If you do not want to use that as an array formula, try my formulas with just enter

Calculating Formula After Text Paste

I have a formula that constructs a vlookup function while drawing the file path from a date input from the user, next I have a macro that takes the results of this formula and pastes them in Cell C6.
=("=" & "IF(ISNA(VLOOKUP($A6," & "'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE)),"""",VLOOKUP($A6,"&"'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE))")
The trouble I am having is finding a way for VBA to activate the formula instead of having it sit there in a text format as:
=IF(ISNA(VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE)),"",VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE))
Afterward I just have a macro autofill it down for the other names between column A6:A37.
If anyone can help me with VBA activating the cell, it would be much appreciated.
If you want to get the value of a cell (which might contain a text string) you use something like:
someVariableEtc = Range("A1").Value
If you want to set a Cell's formula using VBA, you would use something like:
Range("C6").Formula = "some text string"
Combining the two, you could come up with something like:
Range("C6").Formula = Range("A1").Value
Look this up. The evaluate function might be what you're looking for. The simplest way to use it is wrapping an expression in square brackets [].

Use formula as a string

I one cell I have to use several different formula depending on certains case.
I another sheet named Static, I wrote a Matrix of formulas in TEXT for all the cases.
My matrix have Criteria1 in rows Criteria2 in columns
So with INDEX/MATCH i will get the text formula for every scenario Criteria1/Criteria2.
Now I want to Evaluate this formula in text.
I did a custom function in VBA
Function Evalue(ByVal str As String)
Application.Volatile
Evalue = Evaluate(str)
End Function
It works when the function is in one block like SUM CONCATENATE etc... but not when there operation of function + -
Your UDF works for me........in A1 enter the text string:
sum(B2:B5)+sum(C4:C9)
in another cell:
=Evalue(A1) displays the correct value.

How to identify a formula of cell contains defined names in Excel by using VSTO?

Is there any way to identify a formula of excel cell contains defined names by using VSTO?
Or is there any way to replace the defined Excel.Name in formula of a Excel cell when the name of Excel.Name is changed by using VSTO?
thanks,
Yst
I don't know if there is a direct built-in way to do that. However, you should be able to recognize and extract a name in the formula by getting the formula of the cell (Range.Formula), and analyzing the string, recognizing that a sequence of characters should be a named range if:
0) it isn't a number
1) it isn't a built-in function, which you could recognize by the fact that it is followed by a (
2) it isn't a cell name, i.e it isn't written as A1, $A1, A$1 or $A$1
Sounds like a fun Regex project!