Excel VBA to check if value in a group of cells is not equal to 1

n0rv1n

New Member
Joined
Oct 12, 2009
Messages
21
Hi All,

There is a button in my worksheet which will allow user to proceed to computation worksheet.
I would like to create a code which will check the value of a group of cells Range("B2:D2,H2,J2,N2,R2,V2,X2,Z2,AB2,AD2.AF2,AH2,AN2,AP2:AX2").

If the value in any of these cells is equal to 1, message box will ask them to change the value and not allow user to proceed.

I tried with this code but it gives me error.

Code:
Sub Prem_Cal()

If Sheets("PS2010_Rel").Range("B2:D2,H2,J2,N2,R2,V2,X2,Z2,AB2,AD2.AF2,AH2,AN2,AP2:AX2") = 1 Then
        Warning = MsgBox("Please make sure the value is enterred correctly", vbOKOnly + vbCritical + vbDefaultButton1, "Discount Restriction")
        End
    End If

Range("A1").Select
    ActiveWorkbook.Unprotect "msigps10"
    Sheets("Prem").Visible = True
    Sheets("Prem").Activate
    Range("A1").Select
    Sheets("Main").Visible = False
    ActiveWorkbook.Protect "msigps10"
End Sub

Appreciate your guidance.

Thanks.

Norvin
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi

How about something like

<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range, NewRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> Rng = Range("B2:D2,H2,J2,N2,R2,V2,X2,Z2,AB2,AD2.AF2,AH2,AN2,AP2:AX2")<br>    <SPAN style="color:#00007F">With</SPAN> Rng<br>        <SPAN style="color:#00007F">Set</SPAN> NewRng = .Find(what:=1, after:=.Cells(1, .Columns.Count), lookat:=xlWhole)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> NewRng <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#007F00">'Warning = .......</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>
 
Upvote 0
If there could be several '1' cells then maybe find them all at once and advise the user.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Prem_Cal()<br>    <SPAN style="color:#00007F">Dim</SPAN> MyRng <SPAN style="color:#00007F">As</SPAN> Range, OneFound <SPAN style="color:#00007F">As</SPAN> Range, AllOnesFound <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> FirstAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> MyRng = Range("B2:D2,H2,J2,N2,R2,V2,X2,Z2,AB2,AD2,AF2,AH2,AN2,AP2:AX2")<br>    <SPAN style="color:#00007F">With</SPAN> MyRng<br>        <SPAN style="color:#00007F">Set</SPAN> OneFound = .Find(What:=1, LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> OneFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> AllOnesFound = OneFound<br>            FirstAddress = OneFound.Address<br>            <SPAN style="color:#00007F">Do</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> AllOnesFound = Union(AllOnesFound, OneFound)<br>                <SPAN style="color:#00007F">Set</SPAN> OneFound = .FindNext(After:=OneFound)<br>            <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> OneFound.Address <> FirstAddress<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> AllOnesFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">'Proceed with other code here</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        AllOnesFound.Select<br>        MsgBox "Please make sure the values are enterred correctly" & vbLf _<br>            & "See selected cell(s): " & AllOnesFound.Address(0, 0) _<br>            , vbOKOnly + vbCritical + vbDefaultButton1, "Discount Restriction"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><br></FONT>
 
Upvote 0
Hi Peter,

The code works well.
One more question.
If the cell above every cell in the range that is being checked actually contain the definition of the cell, is possible to show that in the error message box instead of just showing the cell address?

Thank you.

Norvin

If there could be several '1' cells then maybe find them all at once and advise the user.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Prem_Cal()<br>    <SPAN style="color:#00007F">Dim</SPAN> MyRng <SPAN style="color:#00007F">As</SPAN> Range, OneFound <SPAN style="color:#00007F">As</SPAN> Range, AllOnesFound <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> FirstAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> MyRng = Range("B2:D2,H2,J2,N2,R2,V2,X2,Z2,AB2,AD2,AF2,AH2,AN2,AP2:AX2")<br>    <SPAN style="color:#00007F">With</SPAN> MyRng<br>        <SPAN style="color:#00007F">Set</SPAN> OneFound = .Find(What:=1, LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> OneFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> AllOnesFound = OneFound<br>            FirstAddress = OneFound.Address<br>            <SPAN style="color:#00007F">Do</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> AllOnesFound = Union(AllOnesFound, OneFound)<br>                <SPAN style="color:#00007F">Set</SPAN> OneFound = .FindNext(After:=OneFound)<br>            <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> OneFound.Address <> FirstAddress<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> AllOnesFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">'Proceed with other code here</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        AllOnesFound.Select<br>        MsgBox "Please make sure the values are enterred correctly" & vbLf _<br>            & "See selected cell(s): " & AllOnesFound.Address(0, 0) _<br>            , vbOKOnly + vbCritical + vbDefaultButton1, "Discount Restriction"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><br></FONT>
 
Upvote 0
Try

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Prem_Cal()<br>    <SPAN style="color:#00007F">Dim</SPAN> MyRng <SPAN style="color:#00007F">As</SPAN> Range, OneFound <SPAN style="color:#00007F">As</SPAN> Range, AllOnesFound <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> FirstAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, Defns <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> MyRng = Range("B2:D2,H2,J2,N2,R2,V2,X2,Z2,AB2,AD2,AF2,AH2,AN2,AP2:AX2")<br>    <SPAN style="color:#00007F">With</SPAN> MyRng<br>        <SPAN style="color:#00007F">Set</SPAN> OneFound = .Find(What:=1, LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> OneFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> AllOnesFound = OneFound<br>            FirstAddress = OneFound.Address<br>            <SPAN style="color:#00007F">Do</SPAN><br>                Defns = Defns & vbLf & OneFound.Offset(-1).Value<br>                <SPAN style="color:#00007F">Set</SPAN> AllOnesFound = Union(AllOnesFound, OneFound)<br>                <SPAN style="color:#00007F">Set</SPAN> OneFound = .FindNext(After:=OneFound)<br>            <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> OneFound.Address <> FirstAddress<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> AllOnesFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">'Proceed with other code here</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        AllOnesFound.Select<br>        MsgBox "Please make sure the values are enterred correctly" & vbLf _<br>            & "See selected cell(s): " & AllOnesFound.Address(0, 0) & Defns _<br>            , vbOKOnly + vbCritical + vbDefaultButton1, "Discount Restriction"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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