vba - 根据第三列的值比较两列

标签 vba excel

我想要做的是创建一个宏来查看列 (AF) 并根据该值将列 (BI)、(BJ) 和/或 (BK) 一起比较,如果为假,则突出显示比较黄色的细胞。我知道这有点难以理解,但这个例子应该有助于澄清:

我的工作表包含以下列:

Column AF    Column BI    Column BJ    Column BK
PRODUCT      Height       Length       Width

我需要一个宏来查看产品类型并比较该产品的尺寸,如下所示:

  - If product = A, then Length = Width, if not then highlight Length and Width Cells
  - If product = B then Length > Width, if not then highlight Length and Width Cells
  - If product = C then Width > Height < Length, if not highlight Length, Width, and Height cells
  - If product - D then Width = Length < Height, if not highlight Width, Length, and/or Height

我的数据从第 3 行开始,到第 5002 行结束。

我尝试对此进行研究,但只能找到比较两个单元格然后写入第三列的解决方案。我可以结合 IF 公式和条件格式来实现此目的,但我不想一直运行此操作,因为工作表将被排序和颜色编码。我计划将此宏放入命令按钮中。

最佳答案

建议将语句(例如Select CaseIf...Then...Else)与运算符And组合在一起。请参阅以下页面:

https://msdn.microsoft.com/en-us/library/office/gg251599.aspx

https://msdn.microsoft.com/en-us/library/office/gg278665.aspx

https://msdn.microsoft.com/EN-US/library/office/gg251356.aspx

之后您应该能够编写类似于以下内容的内容: (下面的代码只是一个示例,它不会工作)

Select Case Product
Case A
    If Length <> Width Then
        Rem Highlight Length And Width  Cells
    End If
Case B
    If Length <= Width Then
        Rem Insert here the code to highlight Length And Width Cells
    End If
Case C
    If Width <= Height And Height >= Length Then
        Rem Insert here the code to highlight Length, Width, and Height cells
    End If
Case D
    If Width <> Length And Length >= Height Then
        Rem Insert here the code to highlight Width, Length, and/or Height
    End If
End Sub

如果您不知道突出显示宽度、长度和高度单元格;我建议在录制宏时手动执行此操作,这将提供一个良好的起点。

关于vba - 根据第三列的值比较两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30601736/

相关文章:

vba - 数组加/减vba

检查工作簿是否存在的 VBA 函数

excel - 如何在 VBA 中使用 COUNTIF?

vba - 创建包含常量或公式的所有单元格的命名范围?

java - 使用apache poi检测excel中的隐藏单元格

Excel for mac 隐藏功能区

excel - 调用将 dict 作为值传递的子过程时,参数不可选

vba - 如何在格式化十进制数字时保留尾随零?

excel - 如何在 VBA 中命名多层字段数据透视表

Excel VBA : Move items from one array to another array