查找函数,基础date值

所以我一直在关于在我的工作表中find最后一个月的问题。“我的工作表中的月份是由EOMONTH公式组成的。 因此,我想找出每个月的基本价值,所以我可以忽略格式化问题。

Function Last_Day_Of_the_Month() Dim WrkMonths As Integer Dim StartDate As Date Dim EndDate As Long StartDate = Now EndDate = WorksheetFunction.EoMonth(StartDate, -1) Cells.Find(What:=EndDate, After:=ActiveCell, LookIn:=xlValues, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False).Activate End Function 

这只是返回错误91,它无法find值。 任何帮助将非常感激。

升技尴尬,因为我可以发誓这个代码没有工作之前…但我解决了我的问题,这里是我使用的代码,如果任何人需要它。

 Sub lastdayofthemonth() Dim StartDate As Date Dim EndDate As Date StartDate = Now EndDate = WorksheetFunction.EoMonth(StartDate, -1) Cells.Find(What:=Format(EndDate, "MMMM YYYY"), After:=ActiveCell, LookIn:=xlValues, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False).Select 

如果您将EndDatevariables设置为Date而不是Long则应该将该值设置为活动工作表。
如果不是,你仍然会得到错误91.为了避免这个设置一个variables来存储find的date,并检查它是否包含任何东西之前激活。

 Sub Last_Day_Of_the_Month() Dim WrkMonths As Integer Dim StartDate As Date Dim EndDate As Date Dim rFound As Range StartDate = Now EndDate = WorksheetFunction.EoMonth(StartDate, -1) Set rFound = Cells.Find(What:=EndDate, After:=ActiveCell, LookIn:=xlValues, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False) If Not rFound Is Nothing Then rFound.Activate End If End Sub