not finding "foundRng"

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
660
Office Version
  1. 2019
Platform
  1. Windows
Hello,

My Private Sub is not finding "foundRng", which is todays date and it does exist in column A (7/16/2021).
Thank you.

Here is the code:
VBA Code:
Private Sub Workbook_Open()
'using todays date as foundRng

Dim foundRng As Range

    Sheets("BJs Income").Select
    Set foundRng = Range("A:A").Find(Date)
   
    If Not (foundRng Is Nothing) Then
          foundRng.Select
          ActiveCell.Offset(0, 11).Select
          Selection.Copy
          ActiveCell.PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
              :=False, Transpose:=False
      Else
      End If
      foundRng.Select
   
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Are you sure it is entered in column A as valid date, and not a text entry?
Does it have a time component that may be being suppressed by the format of the cell?

By the way, you should place your "Select" command that you currently have at the end of your code in your IF block, as you do not want to try to select it if you cannot find it (that will cause an error), i.e.
VBA Code:
Private Sub Workbook_Open()
'using todays date as foundRng

Dim foundRng As Range

    Sheets("BJs Income").Select
    Set foundRng = Range("A:A").Find(Date)
   
    If Not (foundRng Is Nothing) Then
          foundRng.Select
          ActiveCell.Offset(0, 11).Select
          Selection.Copy
          ActiveCell.PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
              :=False, Transpose:=False
          foundRng.Select
      Else
          MsgBox "Cannot find current date in column A"
      End If
       
End Sub
 
Upvote 0
Are you sure it is entered in column A as valid date, and not a text entry?
Does it have a time component that may be being suppressed by the format of the cell?

By the way, you should place your "Select" command that you currently have at the end of your code in your IF block, as you do not want to try to select it if you cannot find it (that will cause an error), i.e.
VBA Code:
Private Sub Workbook_Open()
'using todays date as foundRng

Dim foundRng As Range

    Sheets("BJs Income").Select
    Set foundRng = Range("A:A").Find(Date)
  
    If Not (foundRng Is Nothing) Then
          foundRng.Select
          ActiveCell.Offset(0, 11).Select
          Selection.Copy
          ActiveCell.PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
              :=False, Transpose:=False
          foundRng.Select
      Else
          MsgBox "Cannot find current date in column A"
      End If
      
End Sub
Column A:A is formatted as a Date, and it is a formula (A37=A36+7).
Here is the data:
Cell Formulas
RangeFormula
A33,A36:A39A33=A32+7
B33,B37:B39,B35B33=C33*11.75
G33,G37:G39,G35G33=IF($B33>1000,$B33*0.013822435,"$0.00")
I33,I35:I39I33=B33*0.014505962
J33,J35:J39J33=SUM(G33:I33)
M33,M35:M39M33=A33-12
N33,N35:N39N33=M33+6
A35A35=A33+7
H33,H35:H39H33=B33*0.062011088
H34,H40H34=K34-J34
K33,K35:K39K33=B33-J33
K34K34=SUM(K30:K33)
K40K40=SUM(K35:K39)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H8,H13,H18,H24,H29,H34,H40,H45,H50,H56,H61,H66Cell Value>0textNO
H8,H13,H18,H24,H29,H34,H40,H45,H50,H56,H61,H66Cell Value<0textNO
 
Upvote 0
Try temporarily changing the format of column A to include a time component in your date.
Then what does the data look like?
 
Upvote 0
Hi,
try changing this line

VBA Code:
Set foundRng = Range("A:A").Find(Date)

to this

VBA Code:
Set foundRng = Range("A:A").Find(CStr(Date), LookIn:=xlValues, lookat:=xlWhole)

and see if this resolves your issue

Dave
 
Last edited:
Upvote 0
Solution
Dates are pretty nasty in terms of the find command.

Try this (I started with Joe's version)

VBA Code:
Private Sub Workbook_Open()
'using todays date as foundRng

    Dim foundRng As Range
    Dim findDate As Date                ' XXX Added
    
    Sheets("BJs Income").Select
    findDate = CLng(Date)               ' XXX Added
    Set foundRng = Range("A:A").Find(what:=findDate, LookIn:=xlFormulas) ' XXX Modified
   
    If Not (foundRng Is Nothing) Then
          foundRng.Select
          ActiveCell.Offset(0, 11).Select
          Selection.Copy
          ActiveCell.PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
              :=False, Transpose:=False
          foundRng.Select
      Else
          MsgBox "Cannot find current date in column A"
      End If
       
End Sub
 
Upvote 0
Hi,
try changing this line

VBA Code:
Set foundRng = Range("A:A").Find(Date)

to this

VBA Code:
Set foundRng = Range("A:A").Find(CStr(Date), LookIn:=xlValues, lookat:=xlWhole)

and see if this resolves your issue

Dave
Yes. This resolves the issue.
Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,925
Members
449,195
Latest member
Stevenciu

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