簡體   English   中英

使用VBA匹配excel中兩個不同工作表的兩列的值

[英]match the value of two columns of two different worksheets in excel using 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 "

由於在我嘗試單擊運行程序的按鈕時出現某種類型的不匹配錯誤,請幫助我解決錯誤...

您的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“單詞”,而是 usr 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"

變量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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM