Excel Macro with IF function

jon_vaughan

New Member
Joined
Mar 10, 2013
Messages
5
Hello,

I am having issues with a macro using Excel 2003. The end goal of the macro is to validate an on-site inventory of boxes. Some information about workbook:
  • the button triggering the macro is on the worksheet "MAIN"
  • on the worksheet "On-Site Inventory" there is a count formula in cell B1 that counts the number of boxes entered into the inventory =counta(B3:B2000)
  • on the worksheet "On-Site Inventory" there is an IF formula in cell C1 that =if(A1=B1,TRUE)

The macro is supposed to first have an Input Box pop-up where you enter a manual count of the boxes, and then compare that manual count against the number of boxes entered into the inventory. However even if cells A1 and B1 are equal, and cell C1 says "TRUE", I still get the message box "Error!", when that is only supposed to pop-up if the values in A1 and B1 are not equal. Is this because I have a formula in cell C1? How would I fix this? The macro code is below.

Thanks.

Sub OnSite_Validation()
Sheets("On-Site Inventory").Select
Range("A1").Select
ActiveCell.FormulaR1C1 = InputBox("Number of Physical Boxes")

If Sheets("On-Site Inventory").Range("C1").Value = "TRUE" Then
Sheets("MAIN").Select
MsgBox ("On-Site Inventory Validated!")
End If

Sheets("MAIN").Select
MsgBox ("Error!")

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try something like this...

Code:
[COLOR=darkblue]Sub[/COLOR] OnSite_Validation()
    
    Sheets("On-Site Inventory").Range("A1").Value = InputBox("Number of Physical Boxes")
    
    [COLOR=darkblue]If[/COLOR] Sheets("On-Site Inventory").Range("C1").Value = [COLOR=darkblue]True[/COLOR] [COLOR=darkblue]Then[/COLOR]
        MsgBox "On-Site Inventory Validated!"
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "Error!"
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]


Or just this to compare the input box value directly to the value in cell B1
Code:
[color=darkblue]Sub[/color] OnSite_Validation()
    
    [color=darkblue]If[/color] Sheets("On-Site Inventory").Range("B1").Value = InputBox("Number of Physical Boxes") [color=darkblue]Then[/color]
        MsgBox "On-Site Inventory Validated!"
    [color=darkblue]Else[/color]
        MsgBox "Error!"
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,464
Messages
6,055,578
Members
444,800
Latest member
KarenTheManager

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top