将循环中的单元格与VBA

时间:2015-07-17 18:44:37

标签: excel vba excel-vba

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

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

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

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

这应该有效。我提供了您通知的着色代码:

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