在同一张Excel文件的两张纸上将绿色填充为匹配值,将红色填充为不同值

问题描述 投票:0回答:1

如果两个文件(Sheet1和Sheet1_Compare)中的值相同,则我要填充绿色,如果不匹配,则用红色填充。 Excel文件包含多个工作表,并且它们的比较表都在同一excel中。封面和修订表应排除在外。

 Public Sub Differentiate()

     Dim ws As Worksheet
     Dim wsRow As Integer
     Dim wsCol As Integer
     Dim i As Integer
     Dim j As Integer


    For Each ws In Worksheets

        If ws.Name <> "Cover Sheet" Or ws.Name <> "Revision Sheet" Then
            If InStr(LCase(ws.Name), LCase("Compared")) = 0 Then
            With ws.UsedRange
                wsRow = .Rows.Count
                wsCol = .Columns.Count
            End With
            for i = 1 To wsRow
                for j = 1 To wsCol
                If Sheets(ws).Cells(i,j) = Sheet(ws + "_Compare").Cells(i,j) Then
                    Sheets(ws).Cells(i,j).Interior.ColorIndex = 4 'Green
                    Sheets(ws + "_Compare").Cells(i,j).Interior.ColorIndex = 4 'Green
                Else
                    Sheets(ws).Cells(i,j).Interior.ColorIndex = 3 'Red
                    Sheets(ws + "_Compare").Cells(i,j).Interior.ColorIndex = 3 'Red
                End If
                Next j
            Next i
            End If
        End If
    Next ws

    End Sub
excel vba background-color difference
1个回答
0
投票

进行以下更改。应该可以。

  1. 由于您正在与"_Compare"表进行比较,因此您应将这些表从循环中排除。

    更改此:If ws.Name <> "Cover Sheet" Or ws.Name <> "Revision Sheet"

    If ws.Name <> "Cover Sheet" And ws.Name <> "Revision Sheet" And Not (ws.Name Like "*_Compare")

  2. Sheets(ws)更改为Sheets(ws.Name)

  3. Sheet(ws + "_Compare")更改为Sheets(ws.Name + "_Compare")

编辑代码:

 Public Sub Differentiate()
 Dim ws As Worksheet
 Dim wsRow As Integer
 Dim wsCol As Integer
 Dim i As Integer
 Dim j As Integer

Set ws = ActiveSheet
For Each ws In Worksheets
    If ws.Name <> "Cover Sheet" And ws.Name <> "Revision Sheet" And Not (ws.Name Like "*_Compare") Then
        If InStr(LCase(ws.Name), LCase("Compared")) = 0 Then
            With ws.UsedRange
                wsRow = .Rows.Count
                wsCol = .Columns.Count
            End With
            For i = 1 To wsRow
                For j = 1 To wsCol
                    If Sheets(ws.Name).Cells(i, j) = Sheets(ws.Name + "_Compare").Cells(i, j) Then
                        Sheets(ws.Name).Cells(i, j).Interior.ColorIndex = 4 'Green
                        Sheets(ws.Name + "_Compare").Cells(i, j).Interior.ColorIndex = 4 'Green
                    Else
                        Sheets(ws.Name).Cells(i, j).Interior.ColorIndex = 3 'Red
                        Sheets(ws.Name + "_Compare").Cells(i, j).Interior.ColorIndex = 3 'Red
                    End If
                Next j
            Next i
        End If
    End If
Next ws
End Sub

EDIT:检查工作表是否存在的代码。

Option Explicit

Function WorksheetExists(ByVal WorksheetName As String) As Boolean
Dim Sht As Worksheet

    For Each Sht In ThisWorkbook.Worksheets
        If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then
            WorksheetExists = True
            Exit Function
        End If
    Next Sht
WorksheetExists = False
End Function
© www.soinside.com 2019 - 2024. All rights reserved.