Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Compare

0

Hello! Hope you are well.

I am trying to compare to workbook's Odometer list. This code should create a new workbook to list the match and non-matched odometer number from TruckData compared to TruckData2. I am trying to list TruckData as the active workbook but I am not sure how to set the truckdata workbook initially. I think the code should work after that to result in TruckData Odometer list being compared to TruckData2 Odometer list and a new workbook is created with a list of Match and Non-Match on one worksheet. 

Answer
Discuss

Discussion

Hi John!

Thank you for helping me figure out the problem. I watched some teachexcel videos in addition to other videos and it helped me understand variables. I did use this code from website and I had issues setting it to my variable. Thank you again for helping me and for answering the other question. I did try record marco and try to use match but I think I needed a dynamic range and not a static range for Match. I will read about Compare Spreadsheet since I have access to Microsoft 365. 
Allison999 (rep: 4) Oct 19, '21 at 11:45 pm
Add to Discussion

Answers

0
Selected Answer

Allison

A quick search showed that you used the (copyright?) CompareWorkbooks macro from another site (bettersolutions.com) in your file but inadvertently messed it up- your re-titled sub has now undeclared variables (WS1 and Ws2) and refers to variables from your DoCompare sub. Furthermore,since your macro sheets and TestData 2.xlsx* files both have sheets called Data, CompareWorkbooks tries to add a sheet with the same name as an existing sheet (and so VBA fails).

(* be careful of the space in that name- you got it wrong in DoCompare  macro in your file)

You don't need to activate a worksheet for VBA to use it (and it can slow things down) but you do need to set the variables correctly. In the revised file attached I've replaced your macro with this:

Sub DoCompare()

Dim WS1 As Worksheet, WS2 As Worksheet

Set WS1 = ThisWorkbook.Worksheets("Data")
Set WS2 = Workbooks("TruckData 2.xlsx").Worksheets("Data")

Application.ScreenUpdating = False
Call CompareWorkbooks(WS1, WS2)
Application.ScreenUpdating = True

End Sub
which just compares the Data sheets you want to compare (there's little point trying to compare the Glossary sheets IMHO!). It disables screen updating (so Excel works faster), sends WS1 and WS2 to the CompareWorkbooks macro and once control returns, restores screen updating so you see the new sheet "Data Comparison".

I've also I've restored the original macro as CompareWorkbooks(ByVal WS1 As Worksheet,       ByVal WS2 As Worksheet) but changed the line which creates the new sheet and put it to the end, see bits in bold in the extract below):  

'Worksheets.Add.Name = "Comparison " & Worksheets.Count  ' new book for the results
  Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = WS1.Name & " Comparison" ' new SHEET for the results
(original comment changed since it adds a sheet not a workbook!)

That said, the comparison is cell-by-cell (good to see changes between file version if that's what you want) but it doesn't work to compare just Odometer values between vehicles (which you say you want) unless the vehicles appear on the same rows in eack workbook/sheet.

Please note that since you have Excel 365 you "compare versions of a workbook, analyze a workbook for problems or inconsistencies, or see links between workbooks or worksheets" using the Spreadsheet Inquire add-in. That gives a more intuitive result too. The Microsoft guidance on that is here: Compare workbooks using Spreadsheet Inquire

Hope this helps.

Discuss

Discussion

By the way, you made no comment on (nor selected) my Answer to your previous question Compare phone numbers on different workbooks and transfer results to new workbook

That's not in line with the rules of this Forum so if you don't comment on this solution either, I probably won't spend time answering your future questions.
John_Ru (rep: 6142) Oct 19, '21 at 10:31 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login