can't get find date macro to work

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
821
Office Version
  1. 365
Platform
  1. Windows
Hi

I’m can’t get this macro to work.
I make a new worksheet
I put in today's date with format m/dd/yyyy in A7
The rest of the sheet is blank
I put this macro in sheet1 of the VBA window
VBA Code:
Private Sub Worksheet_activate()
Cells.Find(Date).Select
Set C = [A:A].Find(Format(Date, "m/dd/yyyy"))
If Not C Is Nothing Then C.Select
ActiveWindow.ScrollRow = ActiveCell.Row
ActiveWindow.SmallScroll Down:=-1
End Sub
I save and close the sheet
(yes I made it an macro enabled workbook)
When I open it, the cursor goes to A1
That’s where it was when I saved and closed it
I tried changing Worksheet_activate to Worksheet_open
Nothing is in This workbook
I’m using File open and clicking on the sheet


mike
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I don't think you need the FORMAT function in your find statement. It does not care about the format, only that it is a valid date.

Does this do what you want?
VBA Code:
Private Sub Worksheet_Activate()
    Dim C As Range
    Set C = [A:A].Find(Date)
    If Not C Is Nothing Then C.Select
End Sub
 
Upvote 0
Hi Joe4
it's been awhile
alright to sat happy father's day?
about the macro
nope didn't work
the sheet opens where the cursor was when it was saved and closed

mike
 
Upvote 0
Hi Joe4
it's been awhile
alright to sat happy father's day?
about the macro
nope didn't work
the sheet opens where the cursor was when it was saved and closed

mike
Check the following two things:

1. Are you absolutely certain that you have an entry in column A with the current date, entered as a valid date (and NOT entered as text)?
And there is no time component to this entry?

If it is indeed entered as text, then you would need to use the FORMAT function, like you had originally.
VBA Code:
Private Sub Worksheet_Activate()
    Dim C As Range
    Set C = [A:A].Find(Format(Date, "m/d/yyyy"))
    If Not C Is Nothing Then C.Select
End Sub

2. Do you have any other VBA code in this particular worksheet module which could be interfering with this?

3. Are you sure that you have Events enabled?
 
Upvote 0
I used end down and end up to check that there is nothing else in A:A
I typed in today's date
I used this following macro before I opened the sheet
VBA Code:
Sub enableevents()
Application.enableevents = True
End Sub
I tried it again with and without the format function
I used =today()
and did a paste special to get rid of the formula
and tried it with and without the format function
Then just before i sent this...
I made a macro of the Dim C as Range..etc and put it in a modular
after I opened the sheet ( and the private sub didn't work)
I opened the view macro and clicked on the new macro and run..
it worked.
I took out the format function and it also worked
I tried it a couple of times after saving and closing and then opening it
it worked with the macro in the modular but not the private sub


mike
 
Upvote 0
What is the name of the module you have placed this code in?
In order to get it to work automatically, you MUST put it in the specific SHEET module.
If you put it in any other module (like one you manually created), it will not work.
 
Upvote 0
the Private Sub Worksheet_Activate() is in sheet1
The macro I made of the Dim C as Range..etc, I put in a module1
both are in the workbook that the test sheet is in
 
Upvote 0
the Private Sub Worksheet_Activate() is in sheet1
The macro I made of the Dim C as Range..etc, I put in a module1
both are in the workbook that the test sheet is in
I am not quite sure what you mean, as you only posted one block of code originally.

Can you post the VBA code you have in each place?
 
Upvote 0
the original code i'm having trouble is
VBA Code:
Private Sub Worksheet_Activate()
Dim C As Range
Set C = [A:A].Find(Date)
If Not C Is Nothing Then C.Select
End Sub
Which i put in sheet1 of the workbook (sheet1 is where I typed in today's date in A7
After a couple of your reply s, I made a macro using a part of the Private Sub Worksheet_Activate()
and called it Test and put it in a module
VBA Code:
Sub Test()
Dim C As Range
Set C = [A:A].Find(Date)
If Not C Is Nothing Then C.Select
End Sub
I then ran this test macro after i opened the sheet and the troubled macro did not work and this test macro did work


mike
 
Upvote 0
OK, note that "Worksheet_Activate" only runs when you CHANGE sheets, and that sheet is activated.
If you save your workbook on that sheet, and re-open it, since it is already on that sheet, it will NOT run.
However, if you select another sheet in that workbook and come back to it, then it will run.

If you only have one sheet in your workbook, or want it to run when the workbook is first opened, you should place the body of the code in the "Workbook_Open" event.
Note that code MUST go in the "ThisWorkbook" module, not in any sheet modules.

If you want to code to run whenever the workbook is opened OR when that particular sheet is selected/activated, then put the code in BOTH places.
 
Upvote 0
Solution

Forum statistics

Threads
1,222,115
Messages
6,164,027
Members
451,867
Latest member
csktwyr

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