How to verify information is entered in cell only if adjacent cell is not blank. Excel VBA

1

I have two columns, one(B) with a name and the other (A) with a number. I am trying to make it so an error message will prevent me from closing excel if cells in column A are blank but the adjacent cells in column B are not blank.

For Example, if cell B2 is populated with a name, then the adjacent cell A2 MUST also have a number to go with it...otherwise the user will be prevented from closing excel.

I would also like the cells that are missing information in column A to be highlighted red.

The idea is to prevent users from not entering the information in column A where there is a name present in column b.

Could anyone help me with this?

Thanks

J Doe

Posted 2018-05-16T19:09:00.100

Reputation: 13

To clarify, the highlighting isn't for missing information (all blank A cells initially highlighted), it's only for missing information when B has a value, right? Also, you're saying a visible warning isn't enough, you want to actually prevent closure? – fixer1234 – 2018-05-16T21:06:28.180

Answers

3

You can put this in the ThisWorkbook module in the Project:

enter image description here

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim lastRow As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Sheet1") ' Change this to the sheet you need to check!
lastRow = ws.Range("B" & Rows.Count).End(xlUp).Row

Dim rng As Range, cel As Range
Set rng = ws.Range(ws.Cells(1, 2), ws.Cells(lastRow, 2))

For Each cel In rng
    If cel.Offset(0, -1).Value = "" And cel.Value <> "" Then
        MsgBox (cel.Address & " is empty. Please populate before closing file.")
        cel.Offset(0, -1).Interior.Color = RGB(255, 0, 0)
        Cancel = True
        ' Exit Sub
    End If
Next cel

End Sub

Before you close a sheet, it'll check the range A1:A[last row in col. B] and see if any cells in column A are empty where the column B is not. It will then messagebox you the address to enter info. in.

BruceWayne

Posted 2018-05-16T19:09:00.100

Reputation: 2 508

Thanks for this, Its very close to what i was looking for.

Could you make it so A cells are only highlighted when there is information in the adjacent B cell but not in the A cell. For ex, if there is a name in B cell, but A cell next to it is empty, then the a cell would be highlighted, but once that cell is filled - it would no longer be high lighted.

Also, if its not too difficult - Could you make it so the alert message references the name from the B column adjacent to the missing A cell. So if J smith was in cell B2, and A2 was blank, the message would say "J Smith is missing w.e" – J Doe – 2018-05-16T23:13:07.467

@JDoe actually, thinking about it you just need to keep the message box part, and it can simply say "Please fill in the empty cells.". Then just use Conditional Formatting to highlight empty cells where the next column is not empty. The other stuff is pretty self explanatory, just try first and Google around. – BruceWayne – 2018-05-17T05:14:31.243