Checking values in two cells and proceeding if they match

justme101

Board Regular
Joined
Nov 18, 2017
Messages
67
Office Version
  1. 365
Platform
  1. Windows
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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
 
Upvote 0
Solution
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. :)
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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