Dynamic Path based on Cell Value

Sowelu

New Member
Joined
Mar 18, 2009
Messages
27
Hi,

I have VBA code with array formula that looks up some values from series of spreadsheets. Which spreadsheet to use depends on the values in another column.

How do I make the formula the same for the whole range as it will substitute the necessary path into itself? :confused:


here is an example:

here is a code for VBA:

Code:
Range("I2").Select
    Selection.FormulaArray = _
        "=INDEX('1_Repl'!$C$2:$C$8,MATCH(1,IF('1_Repl'!$A$2:$A$8=H4,IF('1_Repl'!$C$2:$C$8<>0,1)),0))"
    Selection.AutoFill Destination:=Range("I2:I535"), Type:=xlFillDefault
TEST_LOOKUP.xls
ABCDEFGHIJK
11stSpreadsheet2ndSpreadsheet
2
3Number123CodeNumberData2
41230.200.211230.22Code=1,so1_Repl.xls
52340.10.1012340.1
63450002345#N/Ahastoreferto2_Repl.xls
712300.2203456#N/A
8234000.153567#N/Apathhastobedynamic
91230.240.240.243678#N/AValueofG2+"_Repl.xls"
103450.1300
11
1_Repl


Thanks a lot for help!!
 
Aack...that's frustrating. Really shouldn't happen here! Okay, so we take extra precautions:

Code:
Sub TRY()
Dim myCodeNum As String
Dim myFormula As String
Dim ws As Worksheet
Dim LRow As Long, x As Long, y As Long
Dim a() As String
Dim i As Long
Dim myWBPath As String
Dim myWBName As String
Dim wb As Workbook
[COLOR="Blue"]Dim wbStart As Workbook[/COLOR]
Dim intCount As Long

Const ROW_WHERE_FORMULA_STARTS As Long = 2 '//First formula will be in this row
Const COLUMN_TO_PUT_FORMULA_IN As Long = 7 '//Column Number to write formulas in..."A" = 1, "B" = 2 etc.
Const COLUMN_TO_DETERMINE_LAST_ROW_BY As Long = 3 '//Excel will fill formulas down as far as last cell in this column
Const COLUMN_WITH_CODE_NUMBERS As Long = 1 '//Column with number to determine Repl sheet number

'--------------------------------------------

Set ws = ActiveSheet
[COLOR="blue"]Set wbStart = ActiveWorkbook[/COLOR]
Application.DisplayAlerts = False

With ws
    '//FIRST PASS-----------------------------------------------------------
    LRow = .Cells(Rows.Count, COLUMN_TO_DETERMINE_LAST_ROW_BY).End(xlUp).Row
    y = COLUMN_TO_PUT_FORMULA_IN

    For x = ROW_WHERE_FORMULA_STARTS To LRow
        
        '//Get sheet number to use in formula
        myCodeNum = .Cells(x, COLUMN_WITH_CODE_NUMBERS).Value
        
        '//Get workbook Name to use in Formula
        myWBPath = "C:\Work\OTT_Pricing\OTT_Repl_Update\" & myCodeNum & "_Repl.xls"
        myWBName = FileNameOnly(myWBPath)
        
        '//Open workbook, capture its name (to close it later)
        If IsWorkbookOpen(myWBName) Then
            Set wb = Workbooks(myWBName)
        Else
            Workbooks.Open Filename:=myWBPath, ReadOnly:=True
            Set wb = ActiveWorkbook
            [COLOR="blue"]wbStart.Activate[/COLOR]
        End If
        myWBName = wb.Name
        ReDim Preserve a(0 To intCount)
        a(UBound(a)) = myWBName
        intCount = intCount + 1

        '//Create a formula string
        myFormula = "=INDEX([" & myWBName & "]Sheet1'!R1C5:R5000C5,MATCH(1,IF([" & myWBName & "]Sheet1'!R1C1:R5000C1=RC3,IF([" & myWBName & "]Sheet1'!R1C5:R5000C5<>0,1)),0))"

        '//write formula to cell
        .Cells(x, y).FormulaArray = myFormula
    
    Next x

    '//SECOND PASS - hard copy values----------------------------------------
    For x = ROW_WHERE_FORMULA_STARTS To LRow
        .Cells(x, y).Value = .Cells(x, y).Value
        If IsError(.Cells(x, y).Value) Then
            .Cells(x, y).ClearContents
        End If
    Next x
    
    '//THIRD PASS - do something with blank cells----------------------------
    For x = ROW_WHERE_FORMULA_STARTS To LRow
        If Len(.Cells(x, y).Value) = 0 Then
            
            '//Do Something with blank cell
            '//Do Something with blank cell
            myCodeNum = .Cells(x, COLUMN_WITH_CODE_NUMBERS).Value
            
        myFormula = "=INDEX([" & myWBName & "]Sheet1!R2C3:R5000C19,MIN(IF([" & myWBName & "]Sheet1!R2C1:R5000C1=RC3,IF([" & myWBName & "]Sheet1!R2C3:R5000C19>0,ROW([" & myWBName & "]Sheet1!R2C3:R5000C19)-ROW([" & myWBName & "]Sheet1!R2C3)+1))),MATCH(TRUE,INDEX([" & myWBName & "]Sheet1!R2C3:R5000C19,MIN(IF([" & myWBName & "]Sheet1!R2C1:R5000C1=RC3,IF([" & myWBName & "]Sheet1R2C3:R5000C19>0,ROW([" & myWBName & "]Sheet1!R2C3:R5000C19)-ROW([" & myWBName & "]Sheet1!R2C3)+1))),0)>0,0))"
            
            .Cells(x, y).FormulaArray = myFormula
            
        End If
    Next x

    '//close workbooks we opened
    For i = LBound(a) To UBound(a)
        Workbooks(a(i)).Close SaveChanges:=False
    Next i

End With
Application.DisplayAlerts = True

End Sub
'--------------------------------------------------
Function IsWorkbookOpen(strWorkbookName) As Boolean
Dim strTemp As String

On Error Resume Next
strTemp = Workbooks(strTemp).Name

If Err Then
    IsWorkbookOpen = False
Else
    IsWorkbookOpen = True
End If

End Function
'----------------------------------------------
Function FileNameOnly(Arg1 As String) As String
    FileNameOnly = _
        StrReverse(Left(StrReverse(Arg1), InStr(1, StrReverse(Arg1), "\") - 1))
End Function
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Ahh...this was the culprit:
Code:
myWBName = wb.Name

Sorry...another error (but above will correct that anyway now)
 
Upvote 0
I'd try to get the formulas as you wrote them...I was trying to use names without full paths in the formula, but maybe got it wrong. They are long formulas, so with array calculation we may be pressing Excel to the limit. If needed, they could be broken into more than one cell instead of all in one cell.
 
Upvote 0
I'd try to get the formulas as you wrote them...I was trying to use names without full paths in the formula, but maybe got it wrong. They are long formulas, so with array calculation we may be pressing Excel to the limit. If needed, they could be broken into more than one cell instead of all in one cell.

it works, and its running now... scary looking :rolleyes::rolleyes:
it constantly switches between windows... so if the files x_Repl is big it's like a slow heartbeat and if it's small it's like my screen starts blinking :)
but the numbers come out :)

been running for 12 min. now... we'll see how much it'll be when it's all done.
 
Upvote 0
Ok, it ran for 1 hour and 40 min. and I stopped it.
It was almost done, so I think it takes about 2 hours, which is way too long. Anything <=15-20 min is acceptable.

Also, i don't know what it was doing for so long because second part -- second formula for values that were #N/A was not applied.

I think the best way is to open all 13 workbooks I get the data from right away, they will not change and will always be in the same folder.
Then we won't have to spell out the path in the formula..

Here is the first formula:
Works well.

Code:
myFormula = "=INDEX([" _
 & myCodeNum & "_Repl.xls]Sheet1'!R1C5:R5000C5,MATCH(1,IF('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1'!R1C1:R5000C1=RC3,IF('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1'!R1C5:R5000C5<>0,1)),0))"


Here is the second formula:



Code:
"=INDEX('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3:R5000C19,MIN(IF('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C1:R5000C1=RC3,IF('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3:R5000C19>0,ROW('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3:R5000C19)-ROW('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3)+1))),MATCH(TRUE,INDEX('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3:R5000C19,MIN(IF('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C1:R5000C1=RC3,IF('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1R2C3:R5000C19>0,ROW('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3:R5000C19)-ROW('C:\Work\OTT_Pricing\OTT_Repl_Update\[" _
 & myCodeNum & "_Repl.xls]Sheet1!R2C3)+1))),0)>0,0))"


If I try to take out the path to the file in the first formula and run the code with all the workbooks open, it still gives me Unable to set formulaArray error...
and the length of the formula becomes shorter.. I dont know why it wants to see path to the file and doesnt like this:

Code:
myFormula = "=INDEX([" _
 & myCodeNum & "_Repl.xls]Sheet1'!R1C5:R5000C5,MATCH(1,IF([" _
 & myCodeNum & "_Repl.xls]Sheet1'!R1C1:R5000C1=RC3,IF([" _
 & myCodeNum & "_Repl.xls]Sheet1'!R1C5:R5000C5<>0,1)),0))"
 
Upvote 0
If you get a chance, describe what these lookups are doing - I agree that there should be a way to speed this up. Alex.
 
Last edited:
Upvote 0
If you get a chance, describe what these lookups are doing - I agree that there should be a way to speed this up. Alex.

I have a table with rows: items
and columns: 1 to 17.

in the first formula i search in column 3 in a row with matched item, and return the first non-zero value. i couldn't use standard vlookup because it returns the very first value. This formula returns first non-zero value.

i apply second formula if there was no result from the first one:
-- either item was not on my list
-- there were no non-zero values in column 3 for it

to address the second scenario, second formula searches for the first non-zero value for an item across all columns.

i did not write formulas myself, so i dont know how to improve them.
 
Upvote 0
Do you have some sample data from a "x_repl.xls" worksheet...?

(please post nothing confidential in nature though)

Alex
 
Upvote 0
Thanks, Looking now. Are the looked-up values always between 0 and 1? But never 1?

Edit: and are these numbers with many decimals places or are the displayed values the true values?

Edit II: How many formulas are we entering on this sheets- i.e., I understand the X_repl.xls sheets have a lot of data - the lookup tables are large. Is this sheet we are putting the results in also one with very many rows?

Alex.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,878
Messages
6,122,062
Members
449,064
Latest member
scottdog129

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top