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!!
 
Hey Guys,

I noticed that Sowelu posted that the screen is "constantly switching between windows...but the numbers come out."

Would adding:

Application.ScreenUpdating = False

Under the Application.DisplayAlerts = False line

And then:

Application.ScreenUpdating = True

Under the Application.DisplayAlerts = True line help speed things up?
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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.

No, values vary: from 0 to I don't even know... I'd say for sure <100.
Values are true values.
The x_Repl.xls sheets have only data. All sheets have different items but the header rows are the same. Sheets vary in length.

The result sheet loads data from different places. x_Repl sheets is data needed for one the columns.

x_Repl sheets are separated based on what item starts with. So I aligned the files' names and added a column in the result sheet that represents first digit of the item--to have it look up the path.
----------------------

It takes about 5 min to run the first formula..

Maybe like you mentioned splitting the second one up into two cells would help? Or it's just the complexity that would still take time...

Also, I was thinking.. My x_Repl files have some rows with all zeroes in them -- I really don't need to keep them, they just slow things down.
Maybe in those spreadsheet along with my macro for importing data in them, I can run one for checking if a row contains only zeroes in columns say B through H, then delete this row?
That would be helpful to speed it up for sure because today it went pretty fast through smaller files. If you could advise me a formula for this...
 
Upvote 0
Hi, I'll get back to you...today's rotten for spare time. Its a bit of a puzzler - array formulas are one of the "expensive" formulas - meaning they take a lot of processing power. I would probably avoid them if I had more than a few hundred rows. I think you have thousands...

Do you have control over the x_repl.xls sheets...in the sense of could we add a key column with a sequence of numbers to number the rows. This may enable a database style query that may go very quickly - we could use that number sequence or key to find the "first row" in a given column meeting a given criteria - but sql queries don't go by rows the way excel does so we need the numbering.

Deleting rows with all zeros may also be enough to speed this up satisfactorily - not a hard macro to write I guess.

If the macro works, another tactic would be to turn off calculation except at the points where we need to force a calculation - this would stop calculations from being re-run frequently. I don't usually turn off calculation until the macro is tested thoroughly since we are using some hard copying of values etc. which will leave us with uncalculated data and we need to be careful we only hard copy calculated values.

Alex.
 
Upvote 0
Yes, I have control over x_Repl.xls sheets.

Let's try adding column with sequence numbers and deleting all zero rows.

I looked at the data: up to 80-90% are all zero rows.
I did not bother with them since there where no need.

Do you know the formula for this?
example:
Book1
ABCDEFGH
1Number123needtodeleteallzerorows
21230.200.2
3555000thisrowwillbedeleted
42340.10.10
5345000.79
6999000thisrowwillbedeleted
712300.220
8234000.15
91230.240.240.24
1034500.770
11
Sheet1
 
Upvote 0
Hi,
I'm just not going to get to this in a timely way. Sorry for that. Here's as far as I got with this tonight.

Maybe someone can pick it up...in sub ClearZeros I got as far as opening all your workbooks one by one but not the deleting....since there's so many rows it could take a while doing a loop so we could try to do something better.

-------------------------------------------------------------------------
It's just not looking too good right now. I wouldn't have tried using formulas on 15 workbooks with 5000+ rows each....too much. Isn't there someway to get this data into one file? That would help immensely.

Code:
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
Private LRow As Long
Private x As Long
Private y As Long
Private wbStart As Workbook
Private wsStart As Worksheet
Private varFilePaths As Variant
'------------------------------
Sub TRY()
Dim myCodeNum As String
Dim myFormula As String
Dim a() As String
Dim i As Long
Dim myWBPath As String
Dim myWBName As String
Dim intCount As Long


Set wsStart = ActiveSheet
Set wbStart = ActiveWorkbook

Call ClearZeros

End Sub
'----------------------------------------------
Sub [COLOR="blue"]ClearZeros[/COLOR]()
Dim a()

With wsStart
    '//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
        ReDim a(0 To intCount)
        a(UBound(a)) = "C:\Work\OTT_Pricing\OTT_Repl_Update\" & myCodeNum & "_Repl.xls"
        intCount = intCount + 1
    
    Next x
    
    varFilePaths = a
    BubbleSort (varFilePaths)
    FilterForUniques (varFilePaths)

    For x = LBound(varFilePaths) To UBound(varFilePaths)
        Workbooks.Open (varFilePaths(x))
        With ActiveWorkbook.Worksheets(1)
            [COLOR="blue"]'//CODE TO DELETE ROWS HERE!!![/COLOR]
        End With
    Next x
    
End With

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
'-------------------------------------------------------
Private Sub FilterForUniques(ByRef a As Variant)
Dim i As Integer
Dim j As Integer
Dim l As Integer
Dim b() As Variant 'Keep an eye on these data types

a = BubbleSort(a)

l = LBound(a)

i = l
ReDim Preserve b(l To i) 'Seed first value in temp array
b(l) = a(l)

For j = LBound(a) + 1 To UBound(a) 'Add unique values
    If a(j) <> a(j - 1) Then
        i = i + 1
        ReDim Preserve b(l To i)
        b(i) = a(j)
    End If
Next j
a = b 'assign temp array to passed argument
End Sub
'----------------------------------------------------------------------
'From http://support.microsoft.com/kb/133135
Function BubbleSort(ByVal tempArray As Variant) As Variant
Dim Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer
Dim x As Long

' Loop until no more "exchanges" are made.
Do
    NoExchanges = True
    
    ' Loop through each element in the array.
    For i = 1 To UBound(tempArray) - 1
    
        ' Substitution when element is greater than the element following int
        If tempArray(i) > tempArray(i + 1) Then
            NoExchanges = False
            Temp = tempArray(i)
            tempArray(i) = tempArray(i + 1)
            tempArray(i + 1) = Temp
        End If
    
    Next i

Loop While Not (NoExchanges)

BubbleSort = tempArray

End Function


Oh well...here goes nothing...
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

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