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

255

这是我下面的代码,我正在尝试编写一个程序来使用vba使用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 "

当我尝试单击运行该程序的按钮时出现一些类型不匹配错误,请帮助我解决错误...

Shai Rado

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而不是usr (或其他任何东西)。


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

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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

VBA:如何比较两个不同工作表中的两列

验证工作表的两列中的值,并在Excel的不同工作表的另一列中插入值

根据单元格值以更有效的方式删除两个不同工作表上的行[VBA Excel]

如何使用QUERY函数匹配Google表格中不同工作表中的两个单元格?

如何在一个查询中引用来自两个不同工作表的两列

如何在VBA中的不同工作表中比较两个值?

在Google工作表中,有没有一种方法可以将饼图中两个不同工作表中的列合并?

如何使用Excel 2007和Excel 2010打印两个具有不同比例选项的不同工作表?

比较两个不同工作表之间的Excel Windows 7中的帐户并合并到一个列表中

VBA:如何将不同工作表上的两个范围合并为一个,以循环浏览

Excel以匹配两个工作表中的值

excel vba宏,以匹配来自两个不同工作簿的单元格,并相应地进行复制和粘贴

使用VBA比较两个Excel工作表

比较两个不同工作表中的两列,然后突出显示差异

使用for循环和数组比较来自不同工作表的两个单元格

如果来自两个不同工作表的多个条件匹配,则返回相应的列值?

Excel:如何将两个工作表中的两列与条件匹配?

合并两个不同工作表中的文本

比较两个不同工作表中的 2 列

在 Excel 中,如何在两个单独的工作表中匹配两个不同的值后有条件地设置单元格格式?

比较两个不同 Excel 表中的两列

使用 VBA 将符合条件的行复制到 Excel 中的两个或多个不同的工作表中

通过匹配来自两个工作表的列值突出显示行中的差异

如何比较同一工作簿但不同工作表中的两个命名范围?

比较两个不同工作表上的数组并只取唯一值

如何使用vba从word引用excel文件中的两个不同工作表?

Pandas:比较如何比较不同工作表中的两列并返回匹配的值

VBA Excel:在两个不同的工作表之间找到匹配项

EXCEL:从另一张工作表中的一列中的两个不同工作表中的两个不同列返回唯一值