简体   繁体   中英

if range is Nothing Exit sub, userform, specialcells

Goal: Search range for cells containing errors, if found a modeless useform shows and allows you to change the sell to "yes", "no", or "Review later". If no cells with errors are found in range, msgbox appears to let you know, hide userform and exit sub.

Problem: I can not get the If range is nothing hide userform and exit sub to work properly. Whenever I reach the point where all the cell errors are dealt with I get a 1004 error on the range "no cells were found".

Sub UserformYes_no_review()
Dim Custchk As CustomListCheck
Set Custchk = VBA.UserForms.Add(CustomListCheck.Name)

With New CustomListCheck
    Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
    If CheckRange Is Nothing Then
      MsgBox "All items have been accounted for"
      CustomListCheck.Hide
      Exit Sub
    Else
        For Each Cell In CheckRange
            Cell.Select
            If VarType(ActiveCell.Value) = vbError Then
                Custchk.Show vbModeless
            End If

        Next Cell
    End If
End With

End Sub

Private Sub CommandButton1_Click()
ActiveCell.Value = "Yes"
Call UserformYes_no
End Sub

Private Sub CommandButton2_Click()
ActiveCell.Value = "No"
Call UserformYes_no  
End Sub

Private Sub CommandButton3_Click()
ActiveCell.Value = "Review Later"
Call UserformYes_no
End Sub

I have looked through a wealth of Stackoverflow pages and tried all the solutions that i could find and nothing is working. As a side note, I used a userform over a msgbox as I needed this to be modeless.

When ever you are working with SpecialCells , use error handling.

Change

Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)

to

On Error Resume Next
Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0

This solved the problem. There is still an issue where the userform will not hide, but I will post another question for this.

Sub UserformYes_no_review()
Dim Custchk As CustomListCheck
Set Custchk = VBA.UserForms.Add(CustomListCheck.Name)
Set CheckRange = Nothing



With New CustomListCheck
On Error Resume Next
    Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0


    If CheckRange Is Nothing Then
      MsgBox "All items have been accounted for"
      CustomListCheck.Hide
      Exit Sub
    Else
        For Each Cell In CheckRange
            Cell.Select
            If VarType(ActiveCell.Value) = vbError Then
                Custchk.Show vbModeless
            End If

        Next Cell
    End If
End With


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM