-1

So here I have the nth question about the find method. I have read many questions about it and the problems it carries, but still couldnt find a solution to my problem.

I simply want to return the number of row and number of column for a particular value (a date). However, the code runs always the same 91 error (object variable not set) because the find method does not find anything.

I have tried to define the variable as range and change the code by setting the variable (i.e. set daterow = etc.). But the problem persists.

Sub actual_cash_flow()
Dim cfdate As Long
Dim today As Long
Dim daterow As Long
Dim datecolumn As Long

today = Date
cfdate = WorksheetFunction.EoMonth(today, -1)

daterow = Sheet2.Cells.Find(What:=cfdate, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, searchformat:=False).Row
datecolumn = Sheet2.Cells.Find(What:=cfdate).Column


End Sub

I would like to have number of row and number of column in order to identify the cell and then perform some actions.

EDIT:

By editing the code as suggested by @mikku and by debugging the range where there is the value and the variable defined in the code I get the same value, however, I still get nothing as output. So I really dont know where the error is. See the image.

enter image description here

Saverio
  • 111
  • 8
  • 1
    Look here: https://stackoverflow.com/a/56838292/7599798 – FunThomas Jul 10 '19 at 10:06
  • You are looking for a date but you have declared `cfdate as **Long**` – Ron Rosenfeld Jul 10 '19 at 10:20
  • declaring the cfdate as date returns the same error – Saverio Jul 10 '19 at 10:22
  • Check date format – Error 1004 Jul 10 '19 at 10:44
  • https://stackoverflow.com/questions/31186598/find-date-variable-within-a-row-and-return-column-vba/31188450#31188450 – Tragamor Jul 10 '19 at 10:59
  • I cannot reproduce your problem with the information you have provided. Can you upload a workbook that demonstrates the problem (with sensitive information removed) to some sharing site and post a link here? Are your Windows Regional Short Date settings `dd-MMM-yy`? – Ron Rosenfeld Jul 10 '19 at 11:23
  • @RonRosenfeld here you are [link](https://wetransfer.com/downloads/4f45c99c9bd0de3c4cc53baea656f56920190710123246/0dafde24b438b086d576ac121ba263a020190710123246/1ba131). Yes, the format date is as you have written in your comment – Saverio Jul 10 '19 at 12:34
  • @RonRosenfeld however i would like that the find methods works for all formats, not only for a specific one – Saverio Jul 10 '19 at 12:34
  • After looking at your worksheet, I believe your question is a duplicate of [Excel VBA Range.Find Date That is a Formula](https://stackoverflow.com/questions/45639660/excel-vba-range-find-date-that-is-a-formula). My answer explains the issue, and also provides two work-arounds. – Ron Rosenfeld Jul 10 '19 at 13:12

2 Answers2

2

As I pointed out in my answer to a previous question, working with dates and the Range.Find function can be tricky. One of the reasons seems to be that the VBA Date datatype is not the same as the date stored on an Excel worksheet. The latter is a Double formatted to look like a date.

So, especially if you want your .Find method to be independent of the date settings, you are probably best off not using the Range.Find method but rather looping through the data.

In the code below, I show an example of how this might work, making assumptions based on the workbook you provided, and also using a VBA array as this will run much faster than looping through a range on the worksheet:

Sub actualcf()

Dim cfdate As Long  'Yes --Long for this application
Dim daterow As Long
Dim datecolumn As Long
Dim fnd As Range

Dim srchRng As Range
Dim vSrch As Variant
Dim I As Long

With Worksheets("Peschiera CF")

'Find the row with the dates
Set srchRng = .Cells.Find(what:="Yr. Ending", after:=.Cells(1, 1), LookIn:=xlValues)

'Read that row into a VBA array, but only the columns with data
'Note that we are using `.Value2` which has no formatting
If Not srchRng Is Nothing Then
    vSrch = .Range(.Cells(srchRng.Row, 1), .Cells(srchRng.Row, .Columns.Count).End(xlToLeft)).Value2
End If

cfdate = WorksheetFunction.EoMonth(Date, -1)
For I = 1 To UBound(vSrch, 2)
    If vSrch(1, I) = cfdate Then
        daterow = srchRng.Row
        datecolumn = I
    End If
Next I

End With
End Sub
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
0

This code should work:

Sub actual_cash_flow()

Dim cfdate As Date
Dim daterow As Long
Dim datecolumn As Long
Dim fnd As Range

cfdate = WorksheetFunction.EoMonth(Date, -1)

Set fnd = Worksheets("SheetName").Cells.Find(What:=cfdate, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, searchformat:=False)

If Not fnd Is Nothing Then

    daterow = fnd.Row
    datecolumn = fnd.Column

End If

End Sub

Put in your sheet Name in the Code.

The problem was with the setup of variables, you should declare them as date when searching for a Date.

Mikku
  • 6,538
  • 3
  • 15
  • 38
  • nope. It returns nothing. If I search the variable in excel as 30-Jun-19 then it finds it, so there must be a problem with the code. – Saverio Jul 10 '19 at 10:22
  • "dd-mmm-yy" so 30 june 2019 is 30-Jun-19 – Saverio Jul 10 '19 at 12:22
  • You need to share your workbook, otherwise I don't see why it isn't working – Mikku Jul 10 '19 at 15:25
  • here you are [sample](https://wetransfer.com/downloads/4f45c99c9bd0de3c4cc53baea656f56920190710123246/0dafde24b438b086d576ac121ba263a020190710123246/1ba131) – Saverio Jul 10 '19 at 16:41