Excel宏根据列和单元格值更改行的颜色

时间:2017-03-27 15:16:39

标签: excel vba excel-vba

我有以下代码,并希望将其修改为适用于活动工作簿中的所有工作表。此外,它目前执行整行,但是有没有办法将其修改为仅突出显示该行直到数据停止(例如:A-J或A-C等而不是A-〜)?

ManagerOfManagers

6 个答案:

答案 0 :(得分:0)

您可以使用以下内容,您需要更改if声明中的范围。

您尚未在此处提供工作表名称,但您也应该完全引用所有范围。

Sub ChangeColor()
    lRow = Range("A" & Rows.Count).End(xlUp).Row
    Set MR = Range("A2:K2" & lRow)
    For Each cell In MR
        If cell.Value = "CENTRL DISTRICT" Then Range("A" & cell.Row & ":J" & cell.Row).Interior.ColorIndex = 10
        If cell.Value = "KC DISTRICT" Then Range("A" & cell.Row & ":J" & cell.Row).Interior.ColorIndex = 3
        If cell.Value = "NE DISTRICT" Then Range("A" & cell.Row & ":J" & cell.Row).Interior.ColorIndex = 11
        If cell.Value = "SE DISTRICT" Then Range("A" & cell.Row & ":J" & cell.Row).Interior.ColorIndex = 30
        If cell.Value = "ST LOUIS DIST" Then Range("A" & cell.Row & ":J" & cell.Row).Interior.ColorIndex = 12
        If cell.Value = "SW DISTRICT" Then Range("A" & cell.Row & ":J" & cell.Row).Interior.ColorIndex = 13
    Next
End Sub

答案 1 :(得分:0)

试试这个(注意我已经为你的代码添加了声明)。

sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

答案 2 :(得分:0)

这与上面的一些相似...不同之处在于从开始到最后一个使用的列将是hi-lite。它还循环遍历活动工作簿中的所有工作表。它假定区域名称在A列中。

Sub ChangeColor()

    For x = 1 To ActiveWorkbook.Sheets.Count

    Sheets(x).Select

    lRow = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
    lCol = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Column
    Set MR = Range("A2:A" & lRow)

      For Each cell In MR
        If cell.Value = "CENTRL DISTRICT" Then Range(Cells(cell.Row, 1), Cells(cell.Row, lCol)).Interior.ColorIndex = 10
        If cell.Value = "KC DISTRICT" Then Range(Cells(cell.Row, 1), Cells(cell.Row, lCol)).Interior.ColorIndex = 3
        If cell.Value = "NE DISTRICT" Then Range(Cells(cell.Row, 1), Cells(cell.Row, lCol)).Interior.ColorIndex = 11
        If cell.Value = "SE DISTRICT" Then Range(Cells(cell.Row, 1), Cells(cell.Row, lCol)).Interior.ColorIndex = 30
        If cell.Value = "ST LOUIS DIST" Then Range(Cells(cell.Row, 1), Cells(cell.Row, lCol)).Interior.ColorIndex = 12
        If cell.Value = "SW DISTRICT" Then Range(Cells(cell.Row, 1), Cells(cell.Row, lCol)).Interior.ColorIndex = 13
      Next

    Next x

End Sub

答案 3 :(得分:0)

以下代码将遍历ActiveWorkbook中的所有工作表(即使我强烈反对使用ActiveWorkbook),并且每个工作表循环遍历MR范围。

我已使用Ifs替换了您的多个Select Case

<强>代码

Option Explicit

Sub ChangeColor()

Dim lRow As Long, lCol As Long
Dim MR As Range, Cell As Range
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Sheets
    With ws
        lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set MR = .Range("A2:K2" & lRow)
        For Each Cell In MR
            lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' <-- in case the last column is different per row
            Select Case Cell.Value
                Case "CENTRL DISTRICT"
                    Cell.Resize(1, lCol - Cell.Column + 1).Interior.ColorIndex = 10
                Case "KC DISTRICT"
                    Cell.Resize(1, lCol - Cell.Column + 1).Interior.ColorIndex = 3
                Case "NE DISTRICT"
                    Cell.Resize(1, lCol - Cell.Column + 1).Interior.ColorIndex = 11
                Case "SE DISTRICT"
                    Cell.Resize(1, lCol - Cell.Column + 1).Interior.ColorIndex = 30
                Case "ST LOUIS DIST"
                    Cell.Resize(1, lCol - Cell.Column + 1).Interior.ColorIndex = 12
                Case "SW DISTRICT"
                    Cell.Resize(1, lCol - Cell.Column + 1).Interior.ColorIndex = 13
            End Select
        Next Cell
    End With
Next ws

End Sub

答案 4 :(得分:0)

这对你有用。您可以通过更改字母来设置您喜欢的开始和结束点,这是对列字母的引用。 Range("A" & n, "G" & n)这是获得所需内容的简单方法。

希望我帮助过。

    Sub ChangeColor()
    Dim nlast As Long

    Sheets("sheetname").Activate
    Set sht = ActiveWorkbook.ActiveSheet
        nlast = Cells(Rows.Count, "A").End(xlUp).Row

                For n = nlast To 1 Step -1
                 If sht.Cells(n, "A").Value = "CENTRL DISTRICT" Then sht.Range("A" & n, "G" & n).Interior.ColorIndex = 10
                 If sht.Cells(n, "A").Value = "KC DISTRICT" Then sht.Range("A" & n, "G" & n).Interior.ColorIndex = 3
                 If sht.Cells(n, "A").Value = "NE DISTRICT" Then sht.Range("A" & n, "G" & n).Interior.ColorIndex = 11
                 If sht.Cells(n, "A").Value = "SE DISTRICT" Then sht.Range("A" & n, "G" & n).Interior.ColorIndex = 30
                 If sht.Cells(n, "A").Value = "ST DISTRICT" Then sht.Range("A" & n, "G" & n).Interior.ColorIndex = 12
                 If sht.Cells(n, "A").Value = "SW DISTRICT" Then sht.Range("A" & n, "G" & n).Interior.ColorIndex = 13
            Next n
     End Sub

答案 5 :(得分:0)

我做了类似的事情,为Excel中表格的行着色。这种低于算法的方法效率不高,但是可以对其进行优化以进行重大更改。

这将完成您的任务。

Private Sub CommandButton21_Click()
Dim i As Integer
i = 2
Dim previousText As String
Dim interiorColorIndex As Integer
Dim fontColorIndex As Integer
interiorColorIndex = 48
fontColorIndex = 2

Do While Cells(i, 3).Value <> ""
    Dim newText As String
    text = Cells(i, 3).Value
    text = Mid(text, 1, 7)

    If previousText = "" Then previousText = text

    If text = previousText Then
        For j = 1 To 10
            Cells(i, j).Interior.ColorIndex = interiorColorIndex
            Cells(i, j).Font.ColorIndex = fontColorIndex
        Next j
    Else
        previousText = text
        If interiorColorIndex = 25 Then
            interiorColorIndex = 48
        Else
            interiorColorIndex = 25
        End If
        For j = 1 To 10
            Cells(i, j).Interior.ColorIndex = interiorColorIndex
            Cells(i, j).Font.ColorIndex = fontColorIndex
        Next j
    End If

    i = i + 1
Loop

结束字幕