Search Table With Formulas Or Arrays?

BillBundle

New member
Joined
Oct 18, 2020
Messages
5
Reaction score
0
Points
0
Excel Version(s)
2019
Attached is my project for searching a table. I've tried using the Index(), the Match(), and the Vlookup(); but I'm getting frustrated trying to get these to work. Please help.

I'm using Excel 2019.

View attachment My Project.xlsm
 
Last edited by a moderator:
Welcome to the forum. :)

There is little point in showing us formulae that don't work. Instead, manually fill in the results you want and post the workbook again. Explain in the post how you arrived at these results.
 
This gets you the time of day for Mars

=INDEX($L$4:$L$27,MATCH($E19,INDEX($N$4:$T$27,0,MATCH($E$15,$N$3:$T$3,0)),0),1)
 
The search is conducted as follows:

1. The weekday criteria is in cell E15.
2. The Positive Planet criteria is in cell E19.
3. The Negative Planet criteria is in cell E20.
4. First the search has to select the column for the weekday across the top of the table.
5. With the weekday column selected the search has to find the Positive Planet in that column.
6. Once the Positive Planet is found then the cooresponding hour is found in the column named "Hours From Sunrise" and retrieved as an answer in E24.
7. With the same weekday column selected the search has to find the Negative Planet in that column.
8. Once the Negative Planet is found then the cooresponding hour is found in the column named "Hours From Sunrise" and retrieved as an answer in E25.

Welcome to the forum. :)

There is little point in showing us formulae that don't work. Instead, manually fill in the results you want and post the workbook again. Explain in the post how you arrived at these results.
 
Thanks Bob,

Your formula works and it returns the time of day. I didn't phrase my question correctly, since I needed the number of the hour from sunrise. So I changed your formula to this: =INDEX($M$4:$M$27,MATCH($E19,INDEX($N$4:$T$27,0,MATCH($E$15,$N$3:$T$3,0)),0),1) and I have the results that I needed.

This gets you the time of day for Mars

=INDEX($L$4:$L$27,MATCH($E19,INDEX($N$4:$T$27,0,MATCH($E$15,$N$3:$T$3,0)),0),1)
 
Please help me once again. I found it necessary to convert this formula to VBA and I've made a module for it and placed the functions in the cells of my spreadsheet. My VBA conversion has no compile errors, but the functions placed in the cells have no results and as far as I know, I've done everything correctly. I've attached the revised spreadsheet with result cells in a red box.

Notes for cell formulas

1. Formula for Positive Hours and results are in E24.
2. Formula for Negative Hours and results are in E25.

Notes for the VBA function

1. Function PositiveHours() and results for Positive Hours are in H24.
2. Function NegativeHours() and results for Negative Hours are in H25.

Code:
Function PositiveHours()


    Dim PlanetaryMethod As Worksheet
    Dim IndexRng1 As Range, IndexRng2 As Range, MatchRng As Range
      
    Set PlanetaryMethod = ThisWorkbook.Worksheets("Planetary Method")

    Set IndexRng1 = PlanetaryMethod.Range("M4:M27")
    Set IndexRng2 = PlanetaryMethod.Range("N4:T27")
    Set MatchRng = PlanetaryMethod.Range("N3:T3")
       
    Set PositiveHour = PlanetaryMethod.Range("H24")

    Set DayOfWeek = PlanetaryMethod.Range("E15")
    Set PositivePlanet = PlanetaryMethod.Range("E19")

' Original Formula

'=INDEX($M$4:$M$27,MATCH($E19,INDEX($N$4:$T$27,0,MATCH($E$15,$N$3:$T$3,0)),0),1)

 PositiveHour = Application.WorksheetFunction.Index(IndexRng1, _
                Application.WorksheetFunction.Match(PositivePlanet, _
                Application.WorksheetFunction.Index(IndexRng2, 0, _
                Application.WorksheetFunction.Match(DayOfWeek, MatchRng, 0)), 0), 1)
     
End Function

=============================================

Function NegativeHours()

    Dim PlanetaryMethod As Worksheet
    Dim IndexRng1 As Range, IndexRng2 As Range, MatchRng As Range
      
    Set PlanetaryMethod = ThisWorkbook.Worksheets("Planetary Method")

    Set IndexRng1 = PlanetaryMethod.Range("M4:M27")
    Set IndexRng2 = PlanetaryMethod.Range("N4:T27")
    Set MatchRng = PlanetaryMethod.Range("N3:T3")
       
    Set NegativeHour = PlanetaryMethod.Range("H25")

    Set DayOfWeek = PlanetaryMethod.Range("E15")
    Set NegativePlanet = PlanetaryMethod.Range("E20")

' Original Formula

'=INDEX($M$4:$M$27,MATCH($E20,INDEX($N$4:$T$27,0,MATCH($E$15,$N$3:$T$3,0)),0),1)

 NegativeHour = Application.WorksheetFunction.Index(IndexRng1, _
                Application.WorksheetFunction.Match(NegativePlanet, _
                Application.WorksheetFunction.Index(IndexRng2, 0, _
                Application.WorksheetFunction.Match(DayOfWeek, MatchRng, 0)), 0), 1)

End Function

View attachment My Project.xlsmView attachment My Project.xlsm
 
When I first joined VBAExpress forum, somebody had a formula that they wanted converted to VBA, and the respondent basically re-wrote the formula in VBA. I said that I thought that was dumb, formulas are efficient, so why would you call a UDF that then invoked calls back to worksheet functions, before passing the value back to the cell - loads of overheads. Apart from the fact that you are not building good UDFs, a good UDF would pass the variables such as the ranges as parameters not hard-code them, I would still argue that writing a formula in VBA Makes no sense. Without wishing to be as rude to you as I was to them, why would you want to do this?
 
If you've looked at the table that the formula is getting its search result, you will see that the formula only picks out the first planet in the column. Looking further down the table, you'll see that the same planet occurs several times in the same column. The intention of using VBA is ultimately create a loop to select all the occurances and to have the code create a table with a list of all the occurances. I've seen this done on YouTube with other search examples, so when I get this VBA to work, I will be following that YouTube example to create that loop. I'm doing as much of this coding by myself and only asking for help when I get stuck. The end result will be a Result Table that will change everytime that I give the search new parameters.

There are no compiling errors in my code, but it does not yeild the correct results. If you can help me with this, I can move on to code the loop that I need. This is why I'm using VBA.

When I first joined VBAExpress forum, somebody had a formula that they wanted converted to VBA, and the respondent basically re-wrote the formula in VBA. I said that I thought that was dumb, formulas are efficient, so why would you call a UDF that then invoked calls back to worksheet functions, before passing the value back to the cell - loads of overheads. Apart from the fact that you are not building good UDFs, a good UDF would pass the variables such as the ranges as parameters not hard-code them, I would still argue that writing a formula in VBA Makes no sense. Without wishing to be as rude to you as I was to them, why would you want to do this?
 
Back
Top