# Thread: Search Table With Formulas Or Arrays?

1. ## Search Table With Formulas Or Arrays?

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.

My Project.xlsm  Reply With Quote

2. 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.  Reply With Quote

3. 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)  Reply With Quote

4. 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. Originally Posted by Bob Phillips 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)  Reply With Quote

5. 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```
My Project.xlsmMy Project.xlsm  Reply With Quote

6. 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?  Reply With Quote

7. 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. Originally Posted by AliGW 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•