有人可以帮我吗?我收到编译错误。 谢谢!
写在单独的模块中:
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
Dim row As Long, col As Integer
Set report = Workbooks.Add
With ws1.UsedRange
ws1row = Rows.Count
ws1.col = Columns.Count
End With
With ws2.UsedRange
ws2row = Rows.Count
ws2col = Columns.Count
End With
maxrow = ws1row
maxcol = ws1column
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 = ws2.Cells(row, col).Formula
If colval1 <> 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
End
为插入命令按钮而写
Private Sub CommandButton1_Click()
Set myWorkbook1 = Workbooks.Open("C:\Users\testsample.xlsm")
Compare2Worksheets myWorkbook1.Worksheets("Sheet1"), myWorkbook1.Worksheets("Sheet2")
End Sub
请注意,这是我从VBA基础教程获得的代码。
答案 0 :(得分:0)
ws1.col = Columns.Count
更改为ws1col = Columns.Count
以摆脱第一个错误。猜测这是变量名的错字,但是由于col
,编译器正在寻找一种称为"."
的方法或属性。参见第3点。ws1column
更改为ws1col
Option Explicit
放在代码的顶部,以检查这些事情,即上面的错误Cells(row, col)
等语法时,默认使用Activesheet
,这可能不是您想要的工作表。这可能会导致错误。最好明确指定要使用的工作表,然后使用.
语法,例如
With Worksheets("Sheet1")
.Cells(row, col)
End With
.Rows.Count
End
更正为End Sub
,以为我复印有误。刚从注释中注意到,这也应该是End Sub而不是End
。 End
将终止该计划。