0
Sub Compare2sheets()
    Dim ws1 As Workbook, ws2 As Workbook
    Dim s1 As String, s2 As String
    s1 = InputBox("Enter 1st sheet name")
    s2 = InputBox("Enter 2nd sheet name")
    Set ws1 = Sheets("s1")
    Set ws2 = Sheets("s2")
    Dim rcount As Long, ccount As Long
    rcount = ws1.UsedRange.Rows.Count
    ccount = ws1.UsedRange.Columns.Count
    Dim r As Long, c As Integer
    For r = 1 To rcount
        For c = 1 To ccount
            If ws1.Cells(r, c) <> ws2.Cells(r, c) Then
            ws2.Cells(r, c).Interior.ColorIndex = 6
            End If
        Next c
    Next r
    Set ws1 = Nothing
    Set ws2 = Nothing
End Sub

问题:我无法执行上述代码。如何执行此代码?

4

1 回答 1

0

ws1 和 ws2 被声明为工作簿,但应该被声明为工作表。将代码的第一行更改为:

Dim ws1 as Worksheet, ws2 as Worksheet

此外,在引用工作表时,您必须删除双引号:

Set ws1 = Sheets(s1)
Set ws2 = Sheets(s2)
于 2013-08-26T19:40:33.857 回答