0
我試圖比較兩個不同的工作簿,一個名爲「之後」(這是最新的)和另一個名爲「之前」,我想突出顯示差異,因此很容易確定兩者之間有什麼變化。比較使用VBA的兩個工作簿
好了,所以以後我測試了它一下我被困在一個錯誤
「對象不支持此屬性或方法」。
下面是完整的代碼添加了註釋,所以你可以按照我的思維過程:
Sub OpenCsv()
Dim zcf, FolderPath, after, before, shtAfter, shtBefore As String
Dim MotherWB As Workbook, MotherWS As Worksheet
Dim wb As Workbook, ws, worksheetz As Worksheet
Dim oneRange, aCell As Range
Dim rng As Range
Dim Answer As Integer
Dim mycell As Range
Dim mydiffs As Integer
'Sorts Things for MotherWB
Set oneRange = Range("A4:Z9000")
Set aCell = Range("F4")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
'Opens and sets both Workbooks with their respective sheets
FolderPath = Application.ActiveWorkbook.Path
after = FolderPath + "\" + "after.csv"
before = FolderPath + "\" + "before.xlsm"
Workbooks.Open (after)
Set wb = Workbooks("after.csv")
Set ws = wb.Worksheets("after")
Set MotherWB = Workbooks("before.xlsm")
Set MotherWS = MotherWB.Worksheets("before")
'Makes ws looks like MotherWS so we compare them
With ws
Columns("A:Z").AutoFit
Selection.TextToColumns _
Destination:=Range("A1:A9000"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=True, _
Space:=False, _
Other:=False
Set oneRange = Range("A4:Z9000")
Set aCell = Range("F4")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
End With
'Questions if you want to compare both
Answer = MsgBox("Uma vez aberto o relatório deseja comparar os dois?", vbYesNo + vbQuestion, "Comparar")
If Answer = 6 Then
'For each cell in after that is not the same in before, color it yellow
For Each mycell In wb.ws(after).UsedRange
If Not mycell.Value = MotherWB.MotherWS(before).Cells(mycell.row, mycell.Column).Value Then
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(after).Select
End If
End Sub
它得到的錯誤,我從答案返回6後,說什麼我上面所述。我究竟做錯了什麼?
謝謝你,我修好了,我會相應地編輯代碼。 雖然錯誤仍然存在。 – Atheisthotdog
你現在得到錯誤的哪一行? – 99moorem
和我以前一樣,我猜錯了變量的事實並不重要。 它在我返回數字6之後的For Each segment中開始 – Atheisthotdog