Home > Back-end >  VBA how can check IF selection has a blank value in specific column
VBA how can check IF selection has a blank value in specific column

Time:02-01

I want to check if a selection from a user has a blank cell in a column of that selection. The iteration is tricky for me using a selection as opposed to a preset range.

Any help on how to set the range properly would be most appreciated.

Dim cutrng As Range
Dim c As Range, t As Range

Set cutrng = Selection.EntireRow


For Each t In cutrng.Cells
    For Each c In t.Cells
        If IsEmpty(Cells(c, 53).Value) = True Then
            MsgBox ("You have selected lines that do not have data.")
        End
        Else
        End If
    Next c

Next t

CodePudding user response:

Try the following

Set myRow = Selection.Row

If IsEmpty(Cells(myRow, 53)) = True Then
    MsgBox ("You have selected lines that do not have data.")
End If

There is no need to loop if you only want to check the cell in column 53 and you only have one cell selected. If you expect the user to select multiple rows then try:

Dim myRow as Range

For each myRow in Selection.Rows

    If IsEmpty(Cells(myRow, 53)) = True Then
        MsgBox ("You have selected lines that do not have data.")
    End If
Next myRow

CodePudding user response:

Ended up figuring it out. Thanks everyone for your help.

Dim cutrng As Range
Dim g As Long

Set cutrng = Selection.EntireRow

For g = 1 To Selection.Rows.count
If IsEmpty(cutrng.Cells(g, 53)) = True Then
    MsgBox ("You have selected lines that do not have data.")
    End
    Else
    End If
Next g
  • Related