VBA checking if certain cells are empty

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
893
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I was hoping someone would be able to help me in modifying a VBA code

The below code checks if a certain range is blank or not and proceeds accordingly. The range is determined from where the cell is selected (which is why relative references is used) and if Columns B to K are blank in that particular row the macro will not proceed.

What I would like to do is add one more column which is Column S (aka Cells(Selection.Row 19)

How would I go about doing this?

Thank you to any who can help!

VBA Code:
  Dim msg As String, ans As Variant, lr As Long, sh As Worksheet, lo As ListObject
 
  If Application.CountIf(Sheets("NCR Log").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 11)), "") > 0 Then 'this line is what I require modified
  MsgBox "Please Complete all manditory fields (Columns B to K & S)"
Range(Cells(Selection.Row, 17), Cells(Selection.Row, 17)).ClearContents
    Exit Sub
  End If
msg = "Your NCR File will be finalized," & vbCrLf & "" & vbCrLf & " Do you wish to continue?"

    ans = MsgBox(msg, vbYesNo)

    Select Case ans

        Case vbYes
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The simplest would be somerhing like the below.
Your description tends to encourage an interpretation of All fields being blank when in fact per the code you want if "ANY" are blank.
PS: I don't think Countifs will help you since you want an OR condition not an AND condition.

VBA Code:
If Application.CountIf(Sheets("NCR Log").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 11)), "") > 0 _
        Or Sheets("NCR Log").Cells(Selection.Row, "S") = "" Then
  MsgBox "Please Complete all manditory fields (Columns B to K & S)"
 
Upvote 0
Solution
Is this what you are trying? I have commented the code. Let me know if you still have any questions...

VBA Code:
Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim rw As Long
    Dim totalCells As Long
    
    '~~> Set this to the relevant worksheet
    Set ws = Sheet1
    
    '~~> Check if the selection is a valid range
    If TypeName(Selection) <> "Range" Then
        MsgBox "Select a range first."
        Exit Sub
    End If
    
    '~~> Get the row of the selection
    rw = Selection.Row
    
    '~~> Construct your range
    Set rng = ws.Range("A" & rw & ":K" & rw & ",S" & rw)
    'MsgBox rng.Address
    
    '~~> This is the total count of the cells in the above range
    totalCells = rng.Cells.Count
    
    '~~> Here we are checking if the [filled cells = total count of the cells]
    '~~> If it is not same that means there is at least 1 blank cell.
    If Application.WorksheetFunction.CountA(rng) <> totalCells Then
        MsgBox "Please Complete all manditory fields (Columns A to K & S)"
        Exit Sub
    End If
    
    '~~> Rest of the code
End Sub
 
Upvote 0
The simplest would be somerhing like the below.
Your description tends to encourage an interpretation of All fields being blank when in fact per the code you want if "ANY" are blank.
PS: I don't think Countifs will help you since you want an OR condition not an AND condition.

VBA Code:
If Application.CountIf(Sheets("NCR Log").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 11)), "") > 0 _
        Or Sheets("NCR Log").Cells(Selection.Row, "S") = "" Then
  MsgBox "Please Complete all manditory fields (Columns B to K & S)"

This works perfect! Thank you very much 😊
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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