【问题标题】:Comparing cells in a loop with VBA使用 VBA 比较循环中的单元格
【发布时间】:2015-08-05 03:17:05
【问题描述】:

我被要求在 VBA 中编写一个快速宏,用于比较 Excel 电子表格中两个不同单元格中的值,然后如果其中一个单元格的值小于另一个单元格,则将其中一个单元格更改为红色。我能够为一组单元格执行此操作,但还没有弄清楚如何为多个单元格执行此操作。在我的宏中,我将“E37”与“C40”进行比较。每次我为每个值向下移动 7 行时,我都需要对“E44”和“C47”等进行相同的比较。如果单元格为空白,我还需要一个命令来停止例程,因为并非所有电子表格的长度都相同。

我已经获得了一个宏,每次在电子表格中输入值时都会执行该宏。我在工作表级别分配它,只需要找到一种方法来继续比较单元格。请看下面的代码。

Sub colorcellMacro()
    '
    ' colorcellMacro Macro
    ' change background color according to ref length
    '

    Range("E37").Select
    If Range("E37") < Range("C40") Then
         Range("E37").Select
         With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
             Range("H24").Select
         End With
    Else
        Range("E37").Select
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
End Sub

这是我最终使用的,它是两个建议的组合。

'Sub colorcellMacro()
'
' colorcellMacro Macro
' change background color according to ref length
'

'
Dim firstIndex, secIndex As Integer

firstIndex = 37
secIndex = 40

Do While Range("E" & firstIndex).Value <> "" And Range("C" & secIndex).Value <> ""
    If Range("E" & firstIndex).Value < Range("C" & secIndex).Value Then
         Range("E" & firstIndex).Select
         With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
            Range("H24").Select
        End With
Else
    Range("E" & firstIndex).Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End If
firstIndex = firstIndex + 7
secIndex = secIndex + 7

Loop

End Sub

【问题讨论】:

  • 比较是否从 E37 和 C40 开始?例如,表格上没有更高的比较?
  • 条件格式不是更简单的方法吗?

标签: vba excel


【解决方案1】:
Dim firstIndex, secIndex as Integer

firstIndex = 37
secIndex = 40

while Range("E" & firstIndex).Value <> "" and Range("C" & secIndex).value <> "" Then
    ` Do the comparison here
    ` Change the color here
    firstIndex = firstIndex + 7
    secIndex = secIndex + 7
next

试试这个。如果这不起作用,它将是这样或接近它。

【讨论】:

    【解决方案2】:

    这应该可以。我包含了您告知的着色代码:

    Sub colorCellMacro()
    
    Dim firstRow As Integer
    Dim secondRow As Integer
    
    firstRow = 37
    secondRow = 40
    
    Do While Cells(firstRow, 5) <> "" And Cells(secondRow, 3) <> ""
        If Cells(firstRow, 5).Value < Cells(secondRow, 3).Value Then
             Cells(firstRow, 5).Select
             With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 255
                .TintAndShade = 0
                .PatternTintAndShade = 0
                 Range("H24").Select
             End With
        Else
            Cells(firstRow, 5).Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        End If
        firstRow = firstRow + 7
        secondRow = secondRow + 7
    Loop
    
    End Sub
    

    【讨论】:

    • 这是我使用的代码,它似乎工作得很好。
    • @MikeMc 那你能接受答案吗?谢谢:)
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多