Macro to Go To an intended date in a filtered range

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,339
Office Version
  1. 2021
Platform
  1. MacOS
Hi Experts
Recently, I got help from @kevin9999 in the following post


Once I got things working, I thought exploring options and adding more conditions to it.

Now I realized that instead of jumping to closest date for example in my case - rather than jumping to 20Nov (today-30) it jumps to 25 Sep (today-90)

I must have ignored something simple and unable to identify it.

Please help

Thanks a lot

The code I'm using right now is -

VBA Code:
Dim x As Range
        For Each x In Worksheets("Bank").Range("C2", Cells(Rows.Count, "C").End(xlUp))
            If x >= Date And x <= Date + 365 And x.EntireRow.Hidden = False Then
                x.Select
                Exit For
            ElseIf x >= Date - 30 And x <= Date And x.EntireRow.Hidden = False Then
                x.Select
                Exit For
            ElseIf x >= Date - 60 And x <= Date - 30 And x.EntireRow.Hidden = False Then
                x.Select
                Exit For
            ElseIf x >= Date - 90 And x <= Date - 60 And x.EntireRow.Hidden = False Then
                x.Select
                Exit For
            End If
        Next x
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Sanjay, could we see your actual data with an XL2BB sample? Based on what you've said, it's jumping to the 4th conditional match your code looks at?
 
Upvote 0
Hi Sanjay, could we see your actual data with an XL2BB sample? Based on what you've said, it's jumping to the 4th conditional match your code looks at?
@kevin9999 Unfortunately it's a huge database to share XL2BB else I would have

Underneath is a filtered range screenshot where I realised that it was directly jumping to the 4th condition directly.

It would be great if you may help.

Thanks a lot
 

Attachments

  • Screenshot 2022-12-19 at 12.36.20.png
    Screenshot 2022-12-19 at 12.36.20.png
    122.8 KB · Views: 5
Upvote 0
Hi Sanjay, could we see your actual data with an XL2BB sample? Based on what you've said, it's jumping to the 4th conditional match your code looks at?
Here's another screenshot to understand
 

Attachments

  • Screenshot 2022-12-19 at 12.43.06.png
    Screenshot 2022-12-19 at 12.43.06.png
    127.8 KB · Views: 4
Upvote 0
Hi Sanjay, could we see your actual data with an XL2BB sample? Based on what you've said, it's jumping to the 4th conditional match your code looks at?
Just for your understanding @kevin9999 - If I use ' sign before 4 the condition then it select cell that fulfills 3rd condition and so on...

I wonder what mistake I might be doing in syntax or ordering of conditions...
 
Upvote 0
Just out of interest, why do you expect it to jump to 20 November?
 
Upvote 0
Are your dates constants, or the result of formulas?
All past dates are constant

I occasionally use Today()+1 to record some future transactions and them turn them to constant as and when they happen
 
Upvote 0
Give this a try.
VBA Code:
Sub Sanjay()
    Dim a As Range, d As Date
    With Worksheets("Bank").Range("C2", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlCellTypeVisible)
        d = Date
        For d = Date To Date - 10 Step -1   'Is there ever likely to be more than a 10 day gap?
            If Not .Find(d, After:=Range("C2")) Is Nothing Then
                Set a = .Find(d, After:=Range("C2"))
                a.Select
                Exit For
            End If
        Next d
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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