Using VBA to find and select cells in a defined range

Sandman1985

New Member
Joined
Jun 22, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Hope you're all well.

Have been playing with some VBA to try and create a search and select function and really struggling. My spreadsheet is basically a massive table and has dates in Row 8.
With my code below, I have got an input box to come up to enable a user to enter in a date and be taken to the column where that date exists - did try this with a fixed input cell too but failed there.
My dates start at G8 and finish at EAI8 so have tried to define my search range in the code. Each time I run it, i get the "Not Found" message box. If I use the regular Find function, it actually works fine.

Sub Search()
Dim Find As String
Dim Rng1 As Range
FindS = InputBox("Enter the date you want to search")
With Sheets("Scheduler View").Range("G8:EAI8")
Set Rng = .Find(What:=FindString, After:=Range("G8"))
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Not found"
End If
End With
End Sub

Not sure where I have gone wrong, but any help would be greatly appreciated.

Cheers,

Sandman
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Perhaps something like this.

VBA Code:
Sub Search()
    Dim FindS As String
    Dim FindD As Date, I As Long
    Dim VA As Variant
    Dim Found As Boolean
   
    FindS = InputBox("Enter the date you want to search", "Required Date Format: mm/dd/yyy")
   
    If FindS = "" Then
        Exit Sub
    End If
   
    If Not IsDate(FindS) Then
        MsgBox "Error: '" & FindS & "' is not a valid date. " & vbCrLf _
        & "", vbOKOnly Or vbCritical, "Invalid Date"
        Exit Sub
    End If
   
    FindD = DateValue(FindS)
    FindS = Format(FindD, "m/d/yyyy")
   
    With Sheets("Scheduler View").Range("G8:EAI8")
        .NumberFormat = "m/d/yyyy" 'standardize format
        VA = .Value 'load array
       
        'search array
        Found = False
        For I = 1 To UBound(VA, 2)
            If VA(1, I) = FindS Then
                Found = True
                Exit For
            End If
        Next I
       
        'results
        If Not Found Then
            MsgBox FindS & " not found"
        Else
            .Offset(0, I - 1).Resize(1, 1).Select
        End If
    End With
End Sub

(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

 
Upvote 0
Solution
Perhaps something like this.

VBA Code:
Sub Search()
    Dim FindS As String
    Dim FindD As Date, I As Long
    Dim VA As Variant
    Dim Found As Boolean
  
    FindS = InputBox("Enter the date you want to search", "Required Date Format: mm/dd/yyy")
  
    If FindS = "" Then
        Exit Sub
    End If
  
    If Not IsDate(FindS) Then
        MsgBox "Error: '" & FindS & "' is not a valid date. " & vbCrLf _
        & "", vbOKOnly Or vbCritical, "Invalid Date"
        Exit Sub
    End If
  
    FindD = DateValue(FindS)
    FindS = Format(FindD, "m/d/yyyy")
  
    With Sheets("Scheduler View").Range("G8:EAI8")
        .NumberFormat = "m/d/yyyy" 'standardize format
        VA = .Value 'load array
      
        'search array
        Found = False
        For I = 1 To UBound(VA, 2)
            If VA(1, I) = FindS Then
                Found = True
                Exit For
            End If
        Next I
      
        'results
        If Not Found Then
            MsgBox FindS & " not found"
        Else
            .Offset(0, I - 1).Resize(1, 1).Select
        End If
    End With
End Sub

(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)


Thank you so much rlv01. Worked much better than what I was trying to do!
Appreciate it.
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,099
Members
449,205
Latest member
ralemanygarcia

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