VBA Excel – 定义范围在昨天的date

在VBA中,我试图将范围设置为工作簿打开之前的一天(例如,工作簿在13/04/2017上打开,我希望范围为12/04/2017)

我希望VBA在前一天search列C,当它find它时,它会select与它相邻的单元格(A:我最好)。

然后,利用这些信息,我想要设置search范围,然后将其转换为HTML并作为电子邮件发送。

希望我的要求是有道理的。

我会在那里给你90%。 你想要的代码应该是这样的。 根据你的问题,我不完全明白你想要什么变化。

Dim d As Date d = DateAdd("d", -1, Date) Dim x As Range Dim a As String 'Range("e1").Value = d Columns("C:C").Select a = Cells.Find(What:=d, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Address 'Range(a).Offset(0, 31).Range("A1:B1").Select 'adjust this ' x = Range(a).Offset(0, 31).Range("A1:B1").Address 

试试下面的代码(代码注释中的解释):

 Option Explicit Sub FindYesterdaysDateRow() Dim Rng As Range Dim YesterD As Date YesterD = DateAdd("d", -1, Date) ' <-- get yesterday's date ' the following line will work if you have column C formatted as "Date" Set Rng = Range("C:C").Find(What:=YesterD, LookIn:=xlValues, LookAt:=xlWhole) If Not Rng Is Nothing Then '<-- Find was successful Range("A" & Rng.Row & ":I" & Rng.Row).Select Else ' <-- Find was unable to find yesterday's date MsgBox "Yesterday's date not found in Column C" End If End Sub