Hey everyone, thanks in advance for the help.
So I have 2 workbooks, and 1 worksheet in each one. Workbook1 is used for calculations and has all kinds of formulas and things and works as it should. I want to use workbook2 to check the results of the formulas from workbook1. Workbook2 will simply be a list of all of the expected values for the formulas from workbook1. I need to write a simple macro that will simply go down the list in workbook2 and see if the numbers match the values that should be present in each respective cell in workbook1. All I need is a simple pass-fail: if they match, move on to the next one. If they dont, then stop the macro and spit out an error message.
I'd imagine this could probably be solved with some sort of if-then and/or do-while loop?
I've never written a macro on Excel before, so that's why I'm asking for help. Any advice would be very much appreciated. Thank you again, in advance.
--BA Patterson.
Last edited by bapatterson; 07-12-2010 at 01:33 PM.
Why VBA? A regular formula can compare the cells for you.
In Workbook2, add a Sheet2. In cell A1 enter something like this:
=IF(Sheet1!A1 = [Workbook1.xls]Sheet1!A1, "", ADDRESS(ROW(),COLUMN())
Now copy that cell down and across as needed...the ADDRESSES of the problem cells will appear.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
This could possibly be a good alternative if I can't accomplish what I'm trying to do. I appreciate you taking the time to reply.
The reason I'm looking for a macro is because workbook1 actually has many sheets to it (I just need to copy and tweak the macro for each page), and is a heavily used file. I'm trying to put a test button on each sheet of the page that will run the "test macro" and ensure that that page is working properly after each person makes changes and before each usage.
Like I was saying, your method is a very resonable alternative if I am unable to accomplish what I am hoping for. If any further clarification is necessary, please don't hesitate to ask.
--Thanks again,
----BA Patterson.
Check every cell? Or check a particular column? Any restriction here at all on the comparison range?
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
This will simply compare every cell that has a formula in it:
Option Explicit Sub CompareResults() Dim WB1 As Workbook: Set WB1 = Workbooks("Test.xls") Dim WB2 As Workbook: Set WB2 = ThisWorkbook Dim ws1 As Worksheet: Set ws1 = WB1.Sheets("Sheet1") Dim ws2 As Worksheet: Set ws2 = WB2.Sheets("Sheet1") Dim cell As Range For Each cell In ws1.UsedRange.SpecialCells(xlCellTypeFormulas) If ws2.Range(cell.Address).Value <> cell.Value Then MsgBox "Error in cell " & cell.Address GoTo ExitHandler End If Next cell ExitHandler: Set WB1 = Nothing Set WB2 = Nothing Set ws1 = Nothing Set ws2 = Nothing End Sub
Edit the references at the top.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
It varies from sheet to sheet. Most only need 2 columns to be checked, while a slim few need many more than this. It varies from sheet to sheet. In all cases, there will be information above and below the cells that need to be checked.
I have attached an example sheet from what Workbook1 would look like.
In this example, I need to check F5 - G7 and D10 - E56 (with exception of the words).
I hope this answered what you were asking me about.
--Thanks again
----BA Patterson.
In what order would this macro check the cells? By column, row, or what?
I'm asking so that I would know in what order I would need to put my expected values into workbook2.
--Thank you,
----BA Patterson.
Generic "process all cells in the range" goes row by row. Just play try it out and you'll spot the pattern.
This technique will process all cells with formulas in them. All other cells are ignored.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hey thanks, I appreciate it. I'll try it out and let you know how it goes.
Hmmmm. I couldn't quite get this to work. It DID compare the correct 2 sheets that I wanted. It DID stick to only cells with formulas. The problem lies with the actual comparison.
It will not compare the values that the formula returns. The returned values from the formulas on workbook1 and the expected value that I typed into workbook2 are exactly the same number, but it comes up with an error. If I erase the formula and hand-type in the exact same number, the comparison works. What do you suggest?
--Thanks again,
----BA Patterson.
I think your numbers are not exactly the same. Excel is very precise, but we are not and our perceptions frequently let us down.
Let's see some of these formulas... I'll bet you've got values that look like 1-2 decimal values or even whole numbers for the display, but the actual calculated result is not the same as is being shown.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Even before you posted this response, I verified to make sure that this was not the case. I tried a number that had all zeroes in the decimal place. I then expanded out the number upwards of 20 decimal places in order to ensure that the numbers were exact.
I'll keep poking around with it and see if I can get it to take.
--Thank you,
----BA Patterson.
Post a sample workbook so we can look at the problem together.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Here you go. I put a sheet from WB1 and a sheet from WB2 together for this example workbook. The code has been changed accordingly. Please let me know if you have any problems opening/reading/understanding anything.
--Thank you,
----BA Patterson.
This is only one workbook. The macro is designed to compare cells in two workbooks, the workbook the macro is in and another.
According to your edits in the macro, you set WB1 = to ExampleSheet.xls, but that is the name of the macro'd workbook, which is supposed to be WB2.
WB1 and ws1 need to be one workbook.
WB2 (ThisWorkbook) and ws2 need to be the workbook with the macro in it.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks