Code to search for a date in another sheet, then go to that cell

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

I am looking for a custom search macro that does the following:

I enter a date into a custom search box then click search.

Then whichever sheet I am in, the query then only searches Sheet 1 ("Training Log") and makes column H of the row containing that date the active cell.

Hope you can help?

Many thanks
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,

It was not clear from you post which sheet you wanted the active cell on. The code as is will activate the cell on whatever sheet you are on when you search for the date. If you want to activate the cell on the "Training Log" you can delete the two lines highlighted in Red.

Code:
Sub test()


    Dim resp As String, nam As String
    Dim srch As Range
    
[COLOR=#ff0000]    nam = ActiveSheet.Name[/COLOR]
    Application.ScreenUpdating = False
    resp = InputBox("Enter Date")
    Worksheets("Training Log").Activate
    Set srch = Cells(Cells.Find(What:=resp, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row, _
        Cells.Find(What:=resp, SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column)
[COLOR=#ff0000]    Worksheets(nam).Activate[/COLOR]
    Cells(srch.Row, 8).Select
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Hi igold - thank you so much for helping me out.

I deleted the 2 lines in red - my apologies for the confusion.

I ran the amended code and it returned "Object Variable or With Block Variable not set"

Hope that's a simple fix?

Thanks again
 
Upvote 0
You probably searched for a date that did not exist. This should prevent that from happening. You do not have to delete any lines from this code...


Code:
Sub test()


    Dim resp As String, nam As String
    Dim srch As Range
    
    nam = ActiveSheet.Name
    On Error GoTo CleanUp
    Application.ScreenUpdating = False
    resp = InputBox("Enter Date")
    Worksheets("Training Log").Activate
    Set srch = Cells(Cells.Find(What:=resp, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row, _
        Cells.Find(What:=resp, SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column)
    Cells(srch.Row, 8).Select
    Application.ScreenUpdating = True
    Exit Sub
CleanUp:
    MsgBox "Date Not Found"
    Worksheets(nam).Activate
    Application.ScreenUpdating = True
    
End Sub

I hope this helps.
 
Upvote 0
Thanks again - the dates I tried do in fact exist.

Could it be because the dates in Training Log are formatted as Custom, not Date i.e. ddd, d mmm yyyy - I don't really want to change this format - If this is the issue, is there a workaround?

Thanks again!
 
Upvote 0
Hey I've just looked at your code and I think it's searching for a date in column 8, but all the dates are in Column A. I just wanted the active cell to be Column 8 (H) in the searched date row
 
Last edited:
Upvote 0
The code is not just searching Column 8. Can you show me and example of what your are entering into the InputBox to search for...
 
Upvote 0
I have input the valid date 08/11/2018 (and a couple of others, all UK format) in the below formats:

08/11/2018
8/11/2018
08/11/18
8/11/18
08.11.2018
8.11.2018
08.11.18
8.11.18

They all return Date Not Found
 
Last edited:
Upvote 0
Ok, so none of these were found?

Can you show me the date as it appears on your worksheet in your custom format...
 
Upvote 0
That's correct, none of those were found.

The date is displayed in the cell as Thu, 8 Nov 2018 and in the formula bar it's displayed as 08/11/2018
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,714
Members
452,995
Latest member
isldboy

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