【问题标题】:VBA Versions of VLookupVLookup 的 VBA 版本
【发布时间】:2023-03-10 02:26:01
【问题描述】:

如果我在现有问题中遗漏了这一点,我们深表歉意。

基本上我想要实现的是:

  • 返回相关区域的最后两个通过/失败值
  • 如果我以 Area1 为例,我会返回:“FAILFAIL”

表格如上:

  • COLA = 用于计算面积出现次数的计数器(当前为公式)
  • COLB = 实际区域名称(用 VBA 添加)
  • COLC = 审核级别(随 VBA 添加)
  • 冷 = 空白
  • COLE = 日期(使用 VBA 添加)
  • COLF = 通过/失败(通过 VBA 添加)
  • COLG = 根据对最后两个通过/失败值的评估决定是否升级。
  • COLH = 提供依赖于 G 的新 Lvl

我目前正在使用一个庞大的公式,但我想自动化。

所以最终我希望将 G 列和 H 列自动化。

我知道如果我能得到这两个结果,我可以使用如下所示的 IF。但是得到结果是在逃避我。

res1 = [RESULT1]
res2 = [RESULT2]

        
If res1 & res2 = "PASSPASS" Then
        Action = "Promote"
  ElseIf res1 & res2 = "FAILFAIL" Then
        Action = "Demote"
  Else
        Action = "Retest"
End If

【问题讨论】:

  • 您当前的公式很难理解,请添加一个包含列标题和行标题的屏幕截图。还指定在哪个单元格中输入您的公式以及它是否按预期工作。 “通过或失败”列的当前内容似乎不遵循您描述的规则,请发布一致的示例
  • A列最后两行以Area开头的字符串部分后面没有空格,即Area1 - .." instead of Area,这只是一个错字吗? 1 - 1` 像在第一行? @WilliamJarvis

标签: excel vba worksheet-function


【解决方案1】:

使用Dictionary Object 保存通过/失败的最后一个值以进行比较。

Option Explicit
Sub process()
    Dim wb As Workbook, ws As Worksheet
    Dim dict As Object, key As String
    Dim iLastRow As Long, r As Long
    Dim PassFail As String, Action As String, n As Long

    Set dict = CreateObject("Scripting.Dictionary")

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1") ' sheet to process
    iLastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row

    For r = 2 To iLastRow
        key = Trim(ws.Cells(r, "B")) ' area name
        PassFail = UCase(Trim(ws.Cells(r, "F")))
    
        If dict.exists(key) Then
            ' second or more occurance of area name
            If dict(key) = PassFail Then
                 ' same at last time
                If dict(key) = "PASS" Then
                    Action = "Promote"
                Else
                    Action = "Demote"
                End If
            Else
                ' different to last time
                Action = "Retest"
            End If

            ' update col G
            ws.Cells(r, "G") = Action

            ' store pass/fail for next occurance
            dict(key) = PassFail

        Else
            ' first occurance of area name
            dict.Add key, PassFail
            Action = ws.Cells(r, "G")
        End If

        ' update col H
        Select Case Action
            Case "Promote": n = 1
            Case "Demote": n = -1
            Case Else : n = 0
        End Select
        ws.Cells(r, "H") = ws.Cells(r, "C") + n '

    Next
    MsgBox iLastRow - 1 & " rows processed", vbInformation
End Sub

【讨论】:

    【解决方案2】:

    对不起,伙计们,经过一番调整后,我最终得到了这个,也许有点糊涂,但它完全符合我的需要。

        LastArea = Application.WorksheetFunction.CountIf(ws.Range("B:B"), AuditForm.Area1.Value)
    
    last1 = AuditForm.Area1.Value & "-" & LastArea
    last2 = AuditForm.Area1.Value & "-" & LastArea - 1
    results = Application.WorksheetFunction.VLookup(last1, Sheet2.Range("A2:F2000"), 5, False) & Application.WorksheetFunction.VLookup(last2, Sheet2.Range("A2:F1000"), 5, False)
    lev1 = Application.WorksheetFunction.VLookup(last1, Sheet2.Range("A2:F1000"), 3, False)
    lev2 = Application.WorksheetFunction.VLookup(last2, Sheet2.Range("A2:F1000"), 3, False)
    
    If lev1 = lev2 And results = "PASSPASS" Then
            action1 = "Promote"
        ElseIf lev1 = lev2 And results = "FAILFAIL" Then
            action1 = "Demote"
        Else
            action1 = "Retest"
    End If
    
    If action1 = "Promote" Then
           lvl = lvl + 1
      ElseIf action1 = "Demote" Then
           lvl = lvl - 1
      ElseIf action1 = "Retest" Then
           lvl = lvl
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      相关资源
      最近更新 更多