Excel - VBA循环后查找最大变量

时间:2017-07-19 10:18:39

标签: excel vba variables max

我正在尝试最多2或3个不同的变量(变体)。他们都是约会。 我有下面的代码,但我相信,我做的事情非常错误。 你能帮助我吗? 这是代码;

Dim QA As Integer: QA = 0    
For Each j_WS In Array(WS_1, WS_2, WS3) 

Select Case QA 
Case 0: j = 1 
Case 1: j = 2 
Case 2: j = 3 
End Select 

o = 14 + j * 3 
Dim WA1 As Variant 
Dim WA2 As Variant 
Dim WA3 As Variant 
Gorev = Cells(p, o).Address 
SlideNo = Cells(p, 34).Address 
Egitim_Adi = Cells(2, 3).Address 
Dim Satir_bul As Variant 


If Not IsError(Satir_bul) Then 
    Satir_bul = Worksheets(WS_All).Evaluate("=Match(" & Egitim_Adi & "&" & SlideNo & "&" & Gorev & ", '" & j_WS & "'!A:A&'" & j_WS & "'!B:B&'" & j_WS & "'!C:C, 0)") 
Else 
    WA1 = "" 
    WA2 = "" 
    WA3 = "" 
End If 


If j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Yeni Egitim" Or j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Tanitim / Reklam" Then 
    WA1 = Worksheets(j_WS).Cells(Satir_bul, 7).Value 
Else: WA1 = Worksheets(j_WS).Cells(Satir_bul, 9).Value 
    If j = 2 And Worksheets(WS_All).Cells(8, 3).Value = "Yeni Egitim" Or j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Tanitim / Reklam" Then 
        WA2 = Worksheets(j_WS).Cells(Satir_bul, 7).Value 
    Else: WA2 = Worksheets(j_WS).Cells(Satir_bul, 9).Value 
        If j = 3 And Worksheets(WS_All).Cells(8, 3).Value = "Yeni Egitim" Or j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Tanitim / Reklam" Then 
            WA3 = Worksheets(j_WS).Cells(Satir_bul, 7).Value 
        Else: WA3 = Worksheets(j_WS).Cells(Satir_bul, 9).Value 


        End If 
    End If 
End If 


QA = QA + 1 
Next j_WS 


If WA1 > WA2 And WA1 > WA3 Then 
    MAX_DATE = WA1 
Else 
    If WA2 > WA3 And WA2 > WA1 Then 
        MAX_DATE = WA2 
    Else 
        If WA3 > WA2 And WA3 > WA1 Then 
            MAX_DATE = WA3 
        Else 
            MAX_DATE = "" 
        End If 
    End If 
End If 


Worksheets(WS_All).Cells(p, 26).Value = MAX_DATE 


Next p 

正如您所看到的,几乎所有内容都是预定义的整数或类似的东西,WA1,WA2和WA3都是日期,我希望MAX_DATE也是日期。

WA1,WA2和WA3在循环内不正确。 WA1在第一次循环时正确,WA2在第二次循环时正确,依此类推,但例如在第二次循环时,WA1变空。因此,MAX_DATE值无法正常工作。

1 个答案:

答案 0 :(得分:0)

首先,将这些变量声明为Date。然后编译器将知道如何使用<运算符比较它们。这是代码:

Dim d1, d2, d3 As Date

d1 = "06-01-2017"
d2 = "05-01-2018"
d3 = "01-01-1900" 'This can be some default value, you can't pass here empty string ("")

If d1 > d2 Then
    MsgBox ("a")
Else
    MsgBox ("b")
End If

如果您使用Date代替Variant,则消息将为ba则为

由于您需要传递一些默认值,请参阅代码中的命题。它不会影响你的最大值,因为它更像是低价值&#34;日期:)