Please help me to find the date in my code

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
The below code needs to find the specified date but it won`t? Please help it is sending me nuts

VBA Code:
        With Rng
       
        Set D = .Find(What:=YDat, LookIn:=xlValues, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=False)

Sub BOReason()

    Dim Ws             As Worksheet
    Dim LRow           As Long
    Dim x              As Variant, y As Variant, i As Variant
    Dim Rng            As Range, Comparerng As Range, D As Range
    Dim YDat           As Date
    Dim TDat           As Date
    Dim LWorkingDay    As Date
    Dim StartTime      As Double
    Dim SecondsElapsed As Double
   
    StartTime = Timer
   
    With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

       

        Set Ws = ActiveSheet
        LRow = Ws.Range("A1").End(xlDown).Row
        Set Rng = Ws.Range("A1" & LRow)
        YDat = Format(CDate(Application.WorksheetFunction.WorkDay(Date, -1)), "dd/mm/yyyy")
        TDat = CDate(Date)
       
        With Rng
       
        Set D = .Find(What:=YDat, LookIn:=xlValues, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=False)
       
        If Not D Is Nothing Then
        Do
        Set D = .FindNext(D)
        Loop While Not D Is Nothing
        .AutoFilter 1, Format(TDat, "dd/mm/yyyy"), 2, Format(YDat, "dd/mm/yyyy")
        ElseIf D Is Nothing Then
        .AutoFilter 1, Format(TDat, "dd/mm/yyyy")
        End If
       
        End With

          If Ws.Name <> "Summary" And Ws.Name <> "Trend" And Ws.Name <> "Supplier BO" And Ws.Name <> "Dif Depot" Then

            Set Comparerng = .Range("E2:E" & LRow)

                 For Each x In Comparerng.SpecialCells(xlCellTypeVisible)
                For Each y In Comparerng.SpecialCells(xlCellTypeVisible)

                       If x = y Then
                            .Range("J" & y.Row) = .Range("J" & x.Row)
                        End If
                    Next y
               Next x
            End If

        .Range("AA1") = ""
        End With
.
    With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    End With


    Ws.ShowAllData

SecondsElapsed = Round(Timer - StartTime, 2)

MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
With this
VBA Code:
YDat = Format(CDate(Application.WorksheetFunction.WorkDay(Date, -1)), "dd/mm/yyyy")
YDat is text string

With this:
VBA Code:
Set D = .Find(What:=YDat, LookIn:=xlValues, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=False)

is searching text-string (like "23/06/2022") in rng (column A)

Make sure column A contains date in text-string, not date value.
 
Upvote 0
OK Thanks for letting us know. If you want to post what you did to fix your original issue, you can mark your own comment as a solution and it may help others with a similar issue.
 
Upvote 0
Here you go

VBA Code:
Sub BOReason()

    Dim Ws             As Worksheet
    Dim LRow           As Long
    Dim x              As Variant, y As Variant, i As Variant, Dat As Variant
    Dim Rng            As Range, Comparerng As Range
    Dim YDat           As Date
    Dim TDat           As Date
    Dim LWorkingDay    As Date
    Dim StartTime      As Double
    Dim SecondsElapsed As Double
   
    StartTime = Timer
   
    With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

       

        Set Ws = ActiveSheet
        LRow = Ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        YDat = Format(CDate(Application.WorksheetFunction.WorkDay(Date, -1)), "dd/mm/yyyy")
        TDat = CDate(Date)
        Dat = YDat
        
        On Error Resume Next
            For Each Dat In Ws.Range("A2:A" & LRow)
            Set Rng = Ws.Range("A2:A" & LRow).Find(YDat, LookIn:=xlValues, LookAt:=xlWhole)
            Next Dat
        
        With Rng
             If Rng Is Nothing Then
                .AutoFilter 1, Format(TDat, "dd/mm/yyyy")
            Else
                .AutoFilter 1, Format(TDat, "dd/mm/yyyy"), 2, Format(YDat, "dd/mm/yyyy")
             End If
             End With
        
        With Ws
          If Ws.Name <> "Summary" And Ws.Name <> "Trend" And Ws.Name <> "Supplier BO" And Ws.Name <> "Dif Depot" Then

            Set Comparerng = .Range("E2:E" & LRow)

                 For Each x In Comparerng.SpecialCells(xlCellTypeVisible)
                For Each y In Comparerng.SpecialCells(xlCellTypeVisible)

                       If x = y Then
                            .Range("J" & y.Row) = .Range("J" & x.Row)
                        End If
                    Next y
               Next x
            End If

        .Range("AA1") = ""
        End With

    With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    End With


    Ws.ShowAllData

SecondsElapsed = Round(Timer - StartTime, 2)

MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End Sub
 
Upvote 0
I don't whether to leave it alone because it is working for you but just in case you want to know.

This line doesn't do anything:
VBA Code:
Dat = YDat

Even if it did serve a purpose it is overwritten by this:
VBA Code:
For Each Dat In Ws.Range("A2:A" & LRow)

But then there is nothing in the following line that changes as you loop through the cells
VBA Code:
Set Rng = Ws.Range("A2:A" & LRow).Find(YDat, LookIn:=xlValues, LookAt:=xlWhole)
So there it really no benefit to have that inside that For loop.
 
Upvote 0
Solution
Ok, Do it how you think it should be please then send it to me. I admit I am still learning VBA so it would help me to understand coding better
 
Upvote 0
The first 2 lines are just for context.

But you should be able to replace this:
VBA Code:
        YDat = Format(CDate(Application.WorksheetFunction.WorkDay(Date, -1)), "dd/mm/yyyy")
        TDat = CDate(Date)
        Dat = YDat
        
        On Error Resume Next
            For Each Dat In Ws.Range("A2:A" & LRow)
            Set Rng = Ws.Range("A2:A" & LRow).Find(YDat, LookIn:=xlValues, LookAt:=xlWhole)
            Next Dat

With the below and still get the same result.
VBA Code:
        YDat = Format(CDate(Application.WorksheetFunction.WorkDay(Date, -1)), "dd/mm/yyyy")
        TDat = CDate(Date)

        On Error Resume Next
            Set Rng = Ws.Range("A2:A" & LRow).Find(YDat, LookIn:=xlValues, LookAt:=xlWhole)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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