使用VBA匹配excel中两个不同工作表的两列的值

时间:2017-09-26 06:27:13

标签: excel vba excel-vba copy worksheet

这是我下面的代码我试图编写一个程序,使用此代码使用vba匹配两个不同工作表的两列值

Sub Compare2Worksheets(ws1 As Worksheet, ws2 As Worksheet)

Dim ws1row As Long, ws2row As Long, ws1col As Integer, ws2col As Integer
Dim maxrow As Long, maxcol As Integer, colval1 As String, colval2 As String
Dim report As Workbook, difference As Long

Set report = Workbooks.Add
With ws1.UsedRange
    ws1row = .Rows.Count
    ws2col = .Columns.Count
End With
With ws2.UsedRange
    ws2row = .Rows.Count
    ws2col = .Columns.Count
End With
maxrow = ws1row
maxcol = ws1col

If maxrow < ws2row Then maxrow = ws2row
If maxcol < ws2col Then maxcol = ws2col

difference = 0

For col = 1 To maxcol
    For Row = 1 To maxrow
        colval1 = ""
        colval2 = ""
        colval1 = ws1.Cells(Row, col).Formula
        colval2 = ws1.Cells(Row, col).Formula

        If colval <> colval2 Then
            difference = difference + 1
            Cells(Row, col).Formula = colval1 & "<>" & colval2
            Cells(Row, col).Interior.Color = 255
            Cells(Row, col).Font.ColorIndex = 2
            Cells(Row, col).Font.Bold = True
        End If
    Next Row
Next col

Columns("A:B").ColumnWidth = 25
report.Saved = True

If difference = 0 Then
    report.Close False
End If
Set report = Nothing

MsgBox difference & " cells contain different data! ", vbInformation, "Comparing Two Worksheets cells contain different data", vbInformation, "Comparing two worksheet "

End Sub

按钮的代码

Private Sub CommandButton1_Click()

Compare2Worksheets Worksheets("Sheet1"), Worksheets("Sheet2")     

End Sub

我在这里收到错误

MsgBox difference & " cells contain different data! ", vbInformation, "Comparing Two Worksheets cells contain different data", vbInformation, "Comparing two worksheet "

因为某些类型不匹配错误,而我试图点击按钮运行该程序,请帮助我解决错误......

2 个答案:

答案 0 :(得分:1)

您的MsgBox包含的String个参数太多了。尝试将其更改为以下代码:

MsgBox difference & " cells contain different data! ", vbInformation, "Comparing Two Worksheets cells contain different data"

除此之外,你的行:

If colval <> colval2 Then

应该是:

If colval1 <> colval2 Then

另外,尽量不要将Row用作变量,因为它是保存的Excel“单词”,而不是iRow(或其他任何内容)。

尝试下面的代码(代码注释中的解释):

Dim wsResult As Worksheet

Set report = Workbooks.Add
Set wsResult = report.Worksheets(1) ' <-- set the worksheet object

With ws1.UsedRange
    ws1row = .Rows.Count
    ws1col = .Columns.Count '<-- had an error here (was `ws2col`)
End With
With ws2.UsedRange
    ws2row = .Rows.Count
    ws2col = .Columns.Count
End With

' Use Max function 
maxrow = WorksheetFunction.Max(ws1row, ws2row)
maxcol = WorksheetFunction.Max(ws1col, ws2col)

'maxrow = ws1row
'maxcol = ws1col    
'If maxrow < ws2row Then maxrow = ws2row
'If maxcol < ws2col Then maxcol = ws2col

difference = 0

For col = 1 To maxcol
    For iRow = 1 To maxrow
        colval1 = ""
        colval2 = ""
        colval1 = ws1.Cells(iRow, col).Formula
        colval2 = ws2.Cells(iRow, col).Formula ' <-- you had an error here, you used `colval1 = ws1.Cells(Row, col).Formula`

        If colval1 <> colval2 Then '<-- you had an error here (used `If colval <> colval2`)
            difference = difference + 1
            ' don't rely on ActiveSheet, use the wsResult worksheet object
            wsResult.Cells(iRow, col).Formula = colval1 & "<>" & colval2
            wsResult.Cells(iRow, col).Interior.Color = 255
            wsResult.Cells(iRow, col).Font.ColorIndex = 2
            wsResult.Cells(iRow, col).Font.Bold = True
        End If
    Next iRow
Next col

wsResult.Columns("A:B").ColumnWidth = 25
report.Saved = True

If difference = 0 Then
    report.Close False
End If
Set report = Nothing

MsgBox difference & " cells contain different data! ", vbInformation, "Comparing Two Worksheets cells contain different data"

答案 1 :(得分:1)

变量maxcolumn未初始化(请参阅下面的代码中的注释)

With ws1.UsedRange
        ws1row = .Rows.Count
        ws2col = .Columns.Count //it should be: ws1col
    End With
    With ws2.UsedRange
        ws2row = .Rows.Count
        ws2col = .Columns.Count
    End With
    maxrow = ws1row
    maxcol = ws1col