Checking values in two cells and proceeding if they match

justme101

New Member
Joined
Nov 18, 2017
Messages
39
Hello all,

The subject might sound pretty simple to some experts but I am just a beginner at coding and wanted some help here. Let me try to explain the issue as clearly as possible.

1. There is a sheet with some cells which are to be filled up by the user and then a button (called the "SAVE" button) is given which saves the filled-up data to a different sheet in the same workbook and resets the fields.
2. There is a cell (F5) where a drop-down gives the user an option to select a value out of 4 options.
3. If the user selects the value "Others" in F5, he needs to give some details in cell G5 and it CANNOT be left blank and the user gets a popup saying "You need to enter a description if you have selected "Others"".
4. If the user selects any other option in F5, he can leave cell G5 blank.
5. Only if these conditions are fulfilled, the save and reset codes will run, otherwise not.

I wrote some code after searching a lot on Google but could not find something which works for me. Can you guys help me out, please? Thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,220
Something like

VBA Code:
With Workbooks("Book1.xlsm").Worksheets("Sheet1")
    
    If .Range("F5").Value = "Others" and .Range("G5").Value = vbNullstring Then
        MsgBox "G5 needs to be filled"
        Exit Sub
    End If

End With
' the rest of your code
 
Solution

justme101

New Member
Joined
Nov 18, 2017
Messages
39
Something like

VBA Code:
With Workbooks("Book1.xlsm").Worksheets("Sheet1")
  
    If .Range("F5").Value = "Others" and .Range("G5").Value = vbNullstring Then
        MsgBox "G5 needs to be filled"
        Exit Sub
    End If

End With
' the rest of your code

Thanks so much for this. But I think I failed to elaborate point no.4, where if the user has selected any value except "Others" the cell G5 NEEDS to be BLANK, your code skips that check, I just tried it.

I actually wrote some code after I posted the issue here and it partially worked, because I was missing the "Exit Sub" line (silly me) and your code gave me that idea. If you can amend your answer, I'll use that, as it looks cleaner and might help others too or I can go with mine. :)
 

justme101

New Member
Joined
Nov 18, 2017
Messages
39
Also, I have another scenario related to the same issue, with a slight difference. Should I ask about that here or create a new thread?
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,220
Thanks so much for this. But I think I failed to elaborate point no.4, where if the user has selected any value except "Others" the cell G5 NEEDS to be BLANK, your code skips that check, I just tried it.

I actually wrote some code after I posted the issue here and it partially worked, because I was missing the "Exit Sub" line (silly me) and your code gave me that idea. If you can amend your answer, I'll use that, as it looks cleaner and might help others too or I can go with mine. :)
No it doesn't check that scenario, it sounds like an If .... ElseIf ....End If construction would work for that.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,772
Messages
5,855,586
Members
431,746
Latest member
VickyBez

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
Top