Excel data validation against a range with dynamic length on other sheet - excel

I have data for drop down menus on a separate sheet called "DynamicListItems". The data is each on its own column. I can easily reference the data with =OFFSET(DynamicListItems!$D$3;0;0;COUNTA(DynamicListItems!$D:$D)-1;1)
But, I have hundreds of these drop down menus, and each need to be used on a different row. Is there a way to reference the range using the ROW() and COLUMN() or similar way so that I can copy the same formula into each data validation field?
Something like "=OFFSET(DynamicListItems!$" & COLUMN() & "$3;0;0;COUNTA(DynamicListItems!$" & COLUMN() & ":$" & COLUMN() & ")-1;1)" would be great, but Excel takes that as a one list option.

I found a solution. I use a named range with =INDIRECT("DynamicListItems!$" & COLUMN() & "$3:$" & COLUMN() & "$" & COUNTA(INDIRECT("DynamicListItems!$" & COLUMN() & ":$" & COLUMN())))

Related

Referring to sheet by it's variable VBA name in a VLOOKUP formula

I'm writing a macro to automate some manual processes. One of them is to assign a IF-ISNA-VLOOKUP formula to check an intermediate pivot table. I need to do it in VBA.
That's the currently used formula:
Range("N2").FormulaR1C1 = "=IF(
ISNA(VLOOKUP(RC[-6],'[Workbook.xlsx]Sheet1'!R1C1:R20C18,2,FALSE)),
0,
VLOOKUP(RC[-6],'[Workbook.xlsx]Sheet1'!R1C1:R20C18,2,FALSE))"
The pivot table I need to check is declared as a worksheet variable Wb.WsPivot, I need to access it through the access formula (referring it by it's variable name) or through VBA code (assigning values to cells instead of a formula).
You need to split the string (of the formula) at the points where you want to insert the worksheet name and then use & to concatenate the formula strings and the name of the worksheet:
Range("N2").FormulaR1C1 = "=IF(ISNA(VLOOKUP(RC[-6],'[Workbook.xlsx]" & WsPivot.Name & "'!R1C1:R20C18,2,FALSE)),0,VLOOKUP(RC[-6],'[Workbook.xlsx]" & WsPivot.Name & "'!R1C1:R20C18,2,FALSE))"
To replace the workbook too:
Range("N2").FormulaR1C1 = "=IF(ISNA(VLOOKUP(RC[-6],'[" & Wb.Name & "]" & WsPivot.Name & "'!R1C1:R20C18,2,FALSE)),0,VLOOKUP(RC[-6],'[" & Wb.Name & "]" & WsPivot.Name & "'!R1C1:R20C18,2,FALSE))"

Is it possible to use Excel functions to evaluate the result of a function?

I have a formula in I2:
= "=" & "'" & $H2 & $I2 & E2 & "'!" & "$C$2"
It combines the directory, filename, sheet name, and cell location.
Now I'd like to evaluate the result of that formula in J2. Is it possible to do so without VBA or with just a bit of VBA? I need to be able to fill down I2 to I1000 and I personally prefer a non-VBA approach.
For example, let's say the result of I2 is:
='R:\20180220\[Test.xlsb]'!$C$2
Is there a way to actually show the value of that cell in J2? Note that workbook Test.xlsb is closed and won't be opened (because I have thousands of workbooks and cannot open all of them).
I tried EVALUATE(Range.Formula) and ExecuteExcel4Macro() in VBA, and tried to wrap them with a UDF, but both failed.
Here's a kludge that may work for you.
Create the formulas as in your current post. Note that the result in your post lacks a reference to a tab/worksheet --- you likely need to update your formula to include a worksheet. The formula I used in my tests is = "=" & "'" & $PATH & $FILE & $TAB & "'!" & "$C$2" (the same as yours but with a field added for the $TAB).
Copy/paste special values the results. This replaces = "=" & "'" & $PATH & $FILE & $TAB & "'!" & "$C$2" with ='C:\PATH\WORKBOOK\TAB!$C$2 to give the correct formulas. Annoyingly they do not calculate automatically.
To force an update of the calcs, F9 doesn't seem to work, so select the results and do a mock find/replace, say replace $C$2 with $C$2. This doesn't change the formulas but gets the recalc going. You might also be able to just save, close and re-open the workbook.
Hope this helps.

excel vba: insert formula with dynamic cell reference using vba?

I am inserting the following hyperlink as a formula using vba:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\log.txt"",""View Log"")"
This works fine, however if my value in cell C was to change then my hyperlink becomes invalid and won't work because it has retained the value of the cell at the time in which the formula was entered.
I need a way of making a dynamic reference to my cell C in the event that the value should change then my link will also change.
Can someone please show me how to do this? Thanks in advance
Your code is taking the value from column C and building a string using that value that looks like this:
"S:\Tasks\FolderName\log.txt"
Instead, what you want to do is build the following string:
"S:\Tasks\" & C2 & "\log.txt"
To do that, use this VBA code:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\"" & C" & ActiveCell.Row() & " & ""\log.txt"",""View Log"")"

INDIRECT function to specify external reference file

I am using this formula to find the row number of the last valid entry in a sheet:
=SUMPRODUCT(MAX((ROW([A17U.SI.csv]A17U.SI!A:A))*([A17U.SI.csv]A17U.SI!A:A<>"")))
I have a list with several values, including "A17U.SI.csv" and "A17U.SI". Is it possible to reference those with the INDIRECT function?
Also, the reference A:A should not become a string (& A:A), it should remain dynamic so I can drag it to other columns.
Thanks for any input.
=SUMPRODUCT(MAX((ROW(INDIRECT("'[" & A3 & "]" & B3 & "'!" & SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","") & ":" & SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1",""))))*(INDIRECT("'[" & A3 & "]" & B3 & "'!" & SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","") &":" & SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","") )<>"")))
Essentially, you are just needing to make your existing formula dynamic via the INDIRECT function. When using it, you just need to make sure you use the single quotes, brackets, and exclamation point to make the syntax valid. Your goal is simply to reproduce what you had hard-coded in there in your original formula.
The tricky part to your question was actually the need for a dynamic range reference, which makes the formula much uglier because we keep having to include this to get the letters for the range in a way that will be dynamically update:
SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")
For example, if you put the above formula on its own in any cell in the A column, it will evaluate to "A". If you could put that in its own cell in the column, then you could simply reference that cell and it would make the formula a bit easier to read. For example, if we put =SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","") in cell A1, then we could write:
=SUMPRODUCT(MAX((ROW(INDIRECT("'[" & A3 & "]" & B3 & "'!" & A1 & ":" & A1)))*(INDIRECT("'[" & A3 & "]" & B3 & "'!" & A1 &":" & A1 )<>"")))
You asked about the syntax when using INDIRECT in SUMPRODUCT. By the time SUMPRODUCT is involved, if we wrote the INDIRECT part right, it shouldn't be any different syntax than what you already had.

Excel vlookup range issue while adding using VBA

1st VBA code: I am using vlookup formula in VBA, below is the formula I placed in VBA:
Rng.Formula = "=IF(RC" & SeseCol & " ="""","""",IF(LEFT(LOWER(RC" & SeseCol & "),4)=""none"","""",VLOOKUP(LEFT(RC" & SeseCol & ",(FIND("" "",RC" & SeseCol & ",1)-1)),'[HMO Base Rule Picker - Formula.xlsm]BaseRule'!C[-10]:C[-5],2,0)))"
1st VBA output: From the above code I get the below formula in excel which is correct:
=IF($C17 ="","",IF(LEFT(LOWER($C17),4)="none","",VLOOKUP(LEFT($C17,(FIND(" ",$C17,1)-1)),BaseRule!B:G,2,0)))
2nd VBA code: However I wanted make C[-10]:C[-5] to a constant range and tried changing to $B:$G as below VBA code:
Rng.Formula = "=IF(RC" & SeseCol & " ="""","""",IF(LEFT(LOWER(RC" & SeseCol & "),4)=""none"","""",VLOOKUP(LEFT(RC" & SeseCol & ",(FIND("" "",RC" & SeseCol & ",1)-1)),'[HMO Base Rule Picker - Formula.xlsm]BaseRule'!$B:$G,2,0)))"
2nd VBA output: Then I am getting the below formula from the above code which is not working. I am not getting the same as 1st VBA output formula, am I missing something on the 2nd code?
=IF(RC7 ="","",IF(LEFT(LOWER(RC7),4)="none","",VLOOKUP(LEFT(RC7,(FIND(" ",RC7,1)-1)),BaseRule!$B:$G,2,0)))
Use C2:C7 instead in place of C[-10]:C[-5].
Final formula would be:
Rng.FormulaR1C1 = "=IF(RC" & SeseCol & " ="""","""",IF(LEFT(LOWER(RC" & SeseCol & _
"),4)=""none"","""",VLOOKUP(LEFT(RC" & SeseCol & ",(FIND("" "",RC" & SeseCol & _
",1)-1)),'[HMO Base Rule Picker - Formula.xlsm]BaseRule'!C2:C7,2,0)))"
In R1C1 Notation [] brackets are used to indicate relative referencing.
To indicate absolute reference, you just need to indicate the actual column or row number. So for Column B that is C2 which is the 2nd column. For Column G which is the 7th column would be C7.
Also take note that I used FormulaR1C1 property above.
That is the correct property when you're using the R1C1 notation in your formula.

Resources