【问题标题】:Case Statement, for loop and if...else in VBAVBA 中的 Case 语句、for 循环和 if...else
【发布时间】:2020-08-29 20:30:23
【问题描述】:

在上面的列中,我有唯一的日期。

我有一个下拉列表,可以选择任何东西..所以它有 8 个排列 (2^3)..所以我想根据选择提取可能的日期..假设我选择年份作为 2020 年和日期作为 19,那么我将提取符合这两个条件的可能日期..如上图...

现在我使用 8 if elseif-=...end if statment...and for loop..还有其他方法可以做同样的工作吗?我想写一个函数,它将(日、月、年、lastrow)作为参数,并根据可能的日期来计算。谁能告诉我怎么做?

我现在的代码:

Public Sub ProbableDate(CaseNo As Integer, lastrow As Long)
Dim sh As Worksheet, sh1 As Worksheet
Set sh1 = Worksheets("Dashboard")
Set sh = Worksheets("Logical operation")
Dim Y As String, M As String, D As String
Y = sh1.Cells(4, 1).Value
M = sh1.Cells(4, 2).Value
D = sh1.Cells(4, 3).Value
Dim L As Long, i As Long
L = 2
With sh
    .Range("H2:H1048576").Clear
    For i = 2 To lastrow
        Select Case CaseNo
            Case 1
                If Year(.Cells(i, 2).Value) = Y Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
           Case 2
                If MonthName(Month(.Cells(i, 2).Value)) = M Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
           Case 3
                If Day(.Cells(i, 2).Value) = D Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
           Case 4
                If Year(.Cells(i, 2).Value) = Y And MonthName(Month(.Cells(i, 2).Value)) = M Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
           Case 5
                If Year(.Cells(i, 2).Value) = Y And Day(.Cells(i, 2).Value) = D Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
           Case 6
                If Day(.Cells(i, 2).Value) = D And MonthName(Month(.Cells(i, 2).Value)) = M Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
           Case 7
                If Day(.Cells(i, 2).Value) = D And MonthName(Month(.Cells(i, 2).Value)) = M And Year(.Cells(i, 2).Value) = Y Then
                            .Cells(L, 8).Value = .Cells(i, 2).Value
                            L = L + 1
                End If
        Case Else
            MsgBox "Wrong Info"
        End Select
    Next i
End With
End Sub

【问题讨论】:

  • 请立即查看。

标签: excel vba


【解决方案1】:

您可以使用二进制查找表来简化代码。每个CaseNo 都与一组特定的真/假结果匹配,用于日、月和年检查。这些与您的原始地图不同,这是新地图:

CaseNo DMY 0 失败 1 天 2M 3 分 4岁 5日 6 我的 7 DMY

还有代码:

With sh
    .Range("H:H").Clear
    For i = 2 To lastrow
        OK = 0
        If Day(.Cells(i, 2).Value) = D Then OK = OK + 1
        If MonthName(Month(.Cells(i, 2).Value)) = M Then OK = OK + 2
        If Year(.Cells(i, 2).Value) = Y Then OK = OK + 4
        
        If OK = CaseNo Then
            .Cells(L, 8).Value = .Cells(i, 2).Value
            L = L + 1
        Else
            MsgBox "Wrong Info"
        End If
    Next i
End With

【讨论】:

  • 谢谢...我会试试的..这个主意太好了..我喜欢这个主意..
  • 我认为 Elseif OK = 0 then msgbox 不是必需的,因为每次数据不匹配时它都会显示消息...谢谢..请添加 then 关键字,我会接受这个答案..这个主意太好了..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多