我想添加一些VBA代码以比较3列中的日期,并将最大(最新)日期输出到我的工作表中。
我目前有代码比较我定义为变量的2个日期列并将值返回到工作表,但是我需要一点帮助来了解如何比较3个日期列,第三个日期列将是: / p>
PPD_3_Date = Worksheets(“ Data”)。Range(“ AS”&i)
这是我当前正在使用的功能:
Function PPDdate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim i As Long, j As Long, k As Long
j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
PPD_1_Date = Worksheets("Data").Range("AW" & i)
PPD_2_Date = Worksheets("Data").Range("BA" & i)
Entity = Worksheets("Data").Range("J" & i)
Dept = Worksheets("Data").Range("M" & i)
TSpot_Date = Worksheets("Data").Range("AS" & i)
If PPD_1_Date > PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
j = j + 1
Else
If PPD_1_Date < PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
j = j + 1
Else
'If IsEmpty(Worksheets("Data").Range(PPD_1_Date & i).Value) = True And IsEmpty(Worksheets("Data").Range(PPD_2_Date & i).Value) = True Then
'GoTo EmptyRange
'Else
If (InStr(1, Entity, "CNG Hospital") Or InStr(1, Entity, "Home Health") Or InStr(1, Entity, "Hospice") Or InStr(1, Dept, "Volunteers")) And IsEmpty(TSpot_Date) = True Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
k = k + 1
Else
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = TSpot_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AY" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = "NO PPD DATES BUT HAS TSPOT DATE"
j = j + 1
End If
End If
End If
Next i
End Function
这在Office 365客户端上运行。预先感谢。
答案 0 :(得分:4)
已经有一个用于查找范围Application.WorksheetFunction.Max
中最大日期的函数:
只需输入包含日期或日期本身的范围,它就会吐出最大的日期。
要“比较3列中的日期并将最大(最新)日期输出到我的工作表中” ,请执行以下操作,假设您的日期存储在PPData1-3
中
Sub PPDDate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim PPD_3_Date As Date
PPD_1_Date = Sheet1.Range("A2")
PPD_2_Date = Sheet1.Range("B2")
PPD_3_Date = Sheet1.Range("C2")
Sheet1.Range("A1") = Application.WorksheetFunction.Max(PPD_1_Date, PPD_2_Date, PPD_3_Date)
Sheet1.Range("A1").NumberFormat = "dd.mm.yyyy"
End Sub
或者直接将公式直接放入要打印最大公式的单元格中,如屏幕截图所示。
如果您想根据日期最大的日期进行各种操作,我可能会执行以下操作:
Option Explicit
Sub PPDDate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim PPD_3_Date As Date
Dim largest_date As Date
PPD_1_Date = Sheet1.Range("A2")
PPD_2_Date = Sheet1.Range("B2")
PPD_3_Date = Sheet1.Range("C2")
Select Case Application.WorksheetFunction.Max(PPD_1_Date, PPD_2_Date, PPD_3_Date)
Case PPD_1_Date:
' Do stuff for case 1
Debug.Print "1"
Case PPD_2_Date:
' Do stuff for case 2
Debug.Print "2"
Case PPD_3_Date:
' Do stuff for case 3
Debug.Print "3"
Case Else:
MsgBox Prompt:="Something went wrong", Title:="Something went wrong", Buttons:=vbExclamation
End Select
End Sub
如果两个日期相等,那么我相信这是第一种将要执行的情况-如果这很重要,则可能需要稍作更改。