david Jogers
asked on
Macro coming up with run time error despite sheet being there.
I have a macro that comes up with an error and cant workout why.
I have the sheet in book 2 called NEW REC but comes up with a runtime error.
Any ideas.
See in attachement that I have this file in 2nd workbook.
Thanks
image001.png
I have the sheet in book 2 called NEW REC but comes up with a runtime error.
Sub ()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh1a As Worksheet
Dim shary, sh2 As Worksheet, fn As Range, col As Variant, s As Long, colA As Long, colB As Long, colC As Long, colD As Long
Application.Calculation = xlCalculationManual
Set wb1 = Workbooks(1) 'substitute actual name for index #. eg. "MyWkbk.xlsx"
Set wb2 = Workbooks(2) 'always open this workbook after the one called workbooks(1)
Set sh1 = wb1.Sheets("Sheet1")
Set sh1a = wb1.Sheets("Sheet2")
Set sh2 = wb2.Sheets("New Rec")
shary = Array(sh1, sh1a)
With sh2
colA = .Rows(1).Find("NCLB", , xlValues).Column
colB = .Rows(1).Find("MACHINE", , xlValues).Column
colC = .Rows(1).Find("Date", , xlValues).Column
colD = .Rows(1).Find("AMOUNT", , xlValues).Column
End With
col = Array(colA, colB, colC, colD)
For s = LBound(shary) To UBound(shary)
With shary(s)
For Each c In .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
Set fn = sh2.Columns(colA).Find(c.Value, , xlValues)
If Not fn Is Nothing Then
For i = LBound(col) To UBound(col)
If sh1.Cells(c.Row, i + 1) <> sh2.Cells(fn.Row, col(i)) Then
Rows(c.Row).Interior.Color = vbYellow
Exit For
End If
Next
Else
c.EntireRow.Interior.Color = vbYellow
End If
Next
End With
Next
Application.Calculation = xlCalculationAutomatic
End Sub
Any ideas.
See in attachement that I have this file in 2nd workbook.
Thanks
image001.png
What is the run-time error, and on which line are you receiving it (screenshots would be nice).
ASKER
I keep getting run time error when I get to Set sh2 = wb2.Sheets("New Rec"). its called a runtime error
Check that the name tab of the sheet doesn't have any any extra spaces.You could also look in Visual Basic and see the names of the sheets it shows in the 'Project - VBAPrtoject' window.
David
What's the error message?
Have you checked that Workbooks(1) and Workbooks(2) are actually referencing the correct workbooks?
What's the error message?
Have you checked that Workbooks(1) and Workbooks(2) are actually referencing the correct workbooks?
ASKER
When I say workbook 1 and 2 its not actually the workbook names as im trying to run the macro on. Sorry forgot to mention that. I need it for several different named workbooks but all with same layout
ASKER
Have I confused everyone??
I have numerous different workbooks that I need to compare data. Fist work book has data in sheet 1 and has columns with headers. NLC. Date. Machine. Amount in columns A-D.
Workbook 2 has similar data in same layout but in a tab within the workbook called new rec.
What I'm trying to do is to check data in workbook 1 and see if it's with in workbook 2 and if not I need it highlighted in workbook 1.
Data must be an exact match between the 2 workbooks in the line concerned.
When comparing data it will be between the 2 workbooks that I have open.
I need it to look all the way down workbook 2 as data may be on different line to book 1 .
I have numerous different workbooks that I need to compare data. Fist work book has data in sheet 1 and has columns with headers. NLC. Date. Machine. Amount in columns A-D.
Workbook 2 has similar data in same layout but in a tab within the workbook called new rec.
What I'm trying to do is to check data in workbook 1 and see if it's with in workbook 2 and if not I need it highlighted in workbook 1.
Data must be an exact match between the 2 workbooks in the line concerned.
When comparing data it will be between the 2 workbooks that I have open.
I need it to look all the way down workbook 2 as data may be on different line to book 1 .
David
You still haven't told us what the error message is.
You still haven't told us what the error message is.
ASKER
Apologies for delay. Right im getting run time error 1004.
C\users\g\Desktop\test could not be found.
When im opening macro to look for error I come up with the error on the highlighted line in yellow in the attachment.
As stated above im just trying easiest way to compare 2 workbooks and highlight missing stuff from 1st workbook
macro.png
C\users\g\Desktop\test could not be found.
When im opening macro to look for error I come up with the error on the highlighted line in yellow in the attachment.
As stated above im just trying easiest way to compare 2 workbooks and highlight missing stuff from 1st workbook
macro.png
David
To me that looks like there's a filename missing.
Why it's missing is hard to tell but I think it could be because the workbook/worksheet references in the code aren't referencing the correct workbook/worksheet.
To me that looks like there's a filename missing.
Why it's missing is hard to tell but I think it could be because the workbook/worksheet references in the code aren't referencing the correct workbook/worksheet.
David,
The filename in cell H3 does not contain the filename suffix (e.g. ".xlsx").
Sam
The filename in cell H3 does not contain the filename suffix (e.g. ".xlsx").
Sam
ASKER
Excuse me being thick but do I have to put the file names in here or at least for WB1 ??
Set wb1 = Workbooks(1) 'substitute actual name for index #. eg. "MyWkbk.xlsx"
Set wb2 = Workbooks(2) 'always open this workbook after the one called workbooks(1)
Set wb1 = Workbooks(1) 'substitute actual name for index #. eg. "MyWkbk.xlsx"
Set wb2 = Workbooks(2) 'always open this workbook after the one called workbooks(1)
No ... the workbook name goes into the Open statement (Set WBc = Workbooks.Open .... )
ASKER
Really really sorry I can't see that anywhere and I apologise for annoying you all.
I've had a change of thought. Is there anyway that I can have this macro saved in a personal workbook and then when I click on it asked me what workbooks I want to open as they will have different numbers each month?
I then need it to compare data between the two workbooks using the criteria of date , nlc etc to see if anything missing.
I will be honest and as far as I go with macro programming is recording one using record function on excel. It's all above my head. Big thanks in advance
I've had a change of thought. Is there anyway that I can have this macro saved in a personal workbook and then when I click on it asked me what workbooks I want to open as they will have different numbers each month?
I then need it to compare data between the two workbooks using the criteria of date , nlc etc to see if anything missing.
I will be honest and as far as I go with macro programming is recording one using record function on excel. It's all above my head. Big thanks in advance
David ... it would be helpful if you could provide sample workbooks (just a few lines of sample data in each), and show how you would like the differences highlighted.
ASKER
Apologies for delay.
Enclosed is 2 workbooks. What I will do is open the 2 workbooks that I need to compare manually myself
I then need a macro that looks in workbook 1 (which will always have a different name) and then looks in workbook 2 ( of which again will always have a different name) and look in the tab ive selected to compare data between the 2 workbooks. If missing from workbook 2 I need it highlighted in workbook 1.
Data in workbook 1 may be in workbook 2 but not on same line so I need it to look right down the selected sheet in workbook 2 to see if there.
H
SDCI-SUMMARY-1907.xls
shere--2-.xlsx
Enclosed is 2 workbooks. What I will do is open the 2 workbooks that I need to compare manually myself
I then need a macro that looks in workbook 1 (which will always have a different name) and then looks in workbook 2 ( of which again will always have a different name) and look in the tab ive selected to compare data between the 2 workbooks. If missing from workbook 2 I need it highlighted in workbook 1.
Data in workbook 1 may be in workbook 2 but not on same line so I need it to look right down the selected sheet in workbook 2 to see if there.
H
SDCI-SUMMARY-1907.xls
shere--2-.xlsx
So you need all 4 columns to match exactly?
One issue I see is that the first worksheet has no monetary units (is it dollars?), and the second one is in Pound Sterling.
One issue I see is that the first worksheet has no monetary units (is it dollars?), and the second one is in Pound Sterling.
ASKER
yes I need all 4 columns to match exactly. Columns NLC/MACHINE/DATE/AMOUNT
The 1st sheet may of pasted wrong but it will defentally be in £ sterling in the amount column
The 1st sheet may of pasted wrong but it will defentally be in £ sterling in the amount column
ASKER
Sorry ladies and gents but did anyone have any luck as just had to go through 1000 entries manually and got square eyes now...
Take a look at the attached. When opened, it will prompt you for the 2 worksheets you wish to compare within the open workbooks. The sheets being compared may be in the same or different workbooks. You will need to complete a simple preparatory step on the worksheet that you will be searching (worksheet #2) before you begin (make a backup copy first).
On your first line with data (I used line 2, but that may be changed) add the following formula to a new column (I used column E, but that can also be changed):
=A2&B2&TEXT(C2,"m/d/yyyy") &D2
Then copy the line down to the last line with data.
If you want to use a column other than column E to store the above, or if your data begins on a line other than line 2, modify one or both of the constants at the top of frmCompare. Save and reopen the worksheet. The script will highlight in yellow columns A-D of all lines in worksheet 1 that did not exist in worksheet 2.
Please let me know if you have any questions.
CompareWorkSheets.xlsm
On your first line with data (I used line 2, but that may be changed) add the following formula to a new column (I used column E, but that can also be changed):
=A2&B2&TEXT(C2,"m/d/yyyy")
Then copy the line down to the last line with data.
If you want to use a column other than column E to store the above, or if your data begins on a line other than line 2, modify one or both of the constants at the top of frmCompare. Save and reopen the worksheet. The script will highlight in yellow columns A-D of all lines in worksheet 1 that did not exist in worksheet 2.
Please let me know if you have any questions.
CompareWorkSheets.xlsm
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.