Question

I am new to VBA. I have a problem which I believe is very simple, but I am failing foolishly. I have to generate a MsgBox warning the user to enter data in blank cells.

I also have to mention the destination of the blank cell by indicating the parameters in the preceding column. Actually my excel list is very big, however, the image I uploaded demonstrates my needs.

In column A I have entered the name of the companies - Ferrari, Audi and Lamborghini, while in Column B I have mentioned their respective cars. In column C,D,E I have mentioned their sales figure for the months. If the user fails to enter the sales figure for one entry, I need to warn him when he checks the data validity.

The message for this image should be : Please Enter the number of units of Lamborghini Aventador and Lamborghini Veneno sold.

In my image, the yellow highlight indicates the data not entered. In my file I have over only one column ( only one month) for sales, but thousands of rows (for cars).

Snapshot Image

Was it helpful?

Solution

Let's say your data looks like this

enter image description here

Try this

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim lrow As Long, i As Long

    Set ws = ThisWorkbook.Sheets("Sheet3")

    With ws
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 4 To lrow
            If Application.WorksheetFunction.CountA(.Range("C" & i & ":E" & i)) <> 3 Then
                MsgBox "Please Enter the number of units of " & .Range("B" & i).Value & " Sold"
                Exit Sub
            End If
        Next i
    End With

    MsgBox "Data Validated"
End Sub

This is what you get when you run the code

enter image description here

and this when everything is filled

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top