vb question

macangc

Board Regular
Joined
Mar 28, 2006
Messages
140
Office Version
  1. 365
Hi

I would like to know how to do the following
I have a spreadsheet where the user can click on a button and it will find todays date on another sheet. using the code below

Sub MyFind()

' Capture value to find from sheet 1
Dim myFindDate As Date
myFindDate = Sheets("Front").Range("iv1")

' Find value on sheet 2
Sheets("Jan").Activate
On Error GoTo err_chk
Cells.Find(What:=myFindDate, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Offset(1, 1).Activate
On Error GoTo 0


But what I would like to know is how i can have each month e.g. Jan, Feb etc to be in different tabs but use the same button on the first page to find "todays" date

For example if todays date was 18/03/2007 i would click on the button and it would search through each months sheet untill it found the correct date.

hope it makes sense.. thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This should do what you want. I have removed the error check and instead put in a boolean which flags whether or not the date is found. You can always put something else within the final IF clause

Code:
Sub MyFind()
    
    Dim blnFound As Boolean    'flag to see if date is found
    Dim mySheet As Worksheet
    Dim myFindDate As Date
    Dim rngFound As Range      'holds found range
    
    blnFound = False
    
    'Capture value to find from sheet 1
    myFindDate = Sheets("Front").Range("iv1")
    
    
    'loop through all sheets until date is found
    For Each mySheet In ThisWorkbook.Sheets
        
        Set rngFound = mySheet.Cells.Find(What:=myFindDate)
        If Not rngFound Is Nothing And mySheet.Name <> "Front" Then
            blnFound = True
            Exit For
        End If
    Next mySheet
    
    If blnFound Then
        mySheet.Activate
        rngFound.Offset(1, 1).Select
    Else
        MsgBox Format(myFindDate, "dd-mmm-yy") & " not found"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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