Ignoring blank cells

Ian1976

Board Regular
Joined
Feb 4, 2016
Messages
139
Office Version
  1. 365
Platform
  1. Windows
Hi,
I’m using this macro and it works great, but it is giving me an error message if the ranges in m8:m47 on the status sheets 1/2 & 3 are blank?
Can anyone help me out to ignore blank cells in those 3 ranges?

Thanks

Sub NewButton_Click()
Dim fname As String

Application.ScreenUpdating = False

'fname = Range("J1") & Range("N1") & " " & Format(Range("L1"), "dd-mm-yyyy")
'ThisWorkbook.SaveAs ThisWorkbook.Path & "" & fname & ".xlsm", 52

Set cellrange1 = Range("M8:M47").Cells.SpecialCells(xlCellTypeConstants)
If Range("N1").Value = "1530" Then
Range("N1").Value = "0600"
Range("L1").Value = Range("L1").Value + 1
For Each cel In cellrange1
cel.Value = cel.Value + 1
Next
Else
Range("N1").Value = "1530"
End If

Sheets("Status Sheet 3502").Select
Set cellrange2 = Range("M8:M44").Cells.SpecialCells(xlCellTypeConstants)
If Range("N1").Value = "0600" Then
For Each cel1 In cellrange2
cel1.Value = cel1.Value + 1
Next
End If
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How about
Code:
On Error Resume Next
Set cellrange1 = Range("M8:M47").Cells.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If Not cellrange1 Is Nothing Then
   If Range("N1").Value = "1530" Then
      Range("N1").Value = "0600"
      Range("L1").Value = Range("L1").Value + 1
      For Each cel In cellrange1
         cel.Value = cel.Value + 1
      Next
   Else
      Range("N1").Value = "1530"
   End If
End If

Sheets("Status Sheet 3502").Select
On Error Resume Next
Set cellrange2 = Range("M8:M44").Cells.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If Not cellrange2 Is Nothing Then
   If Range("N1").Value = "0600" Then
      For Each cel1 In cellrange2
         cel1.Value = cel1.Value + 1
      Next
   End If
End If
 
Upvote 0
Maybe add some error handling code to specifically ignore just that specific error, and continue on. We can also denote that since there is no data found, to skip the For.Next loop, like that:
Code:
Sub NewButton_Click()

Dim fname As String
Dim dataFound As Boolean

Application.ScreenUpdating = False

'fname = Range("J1") & Range("N1") & " " & Format(Range("L1"), "dd-mm-yyyy")
'ThisWorkbook.SaveAs ThisWorkbook.Path & "" & fname & ".xlsm", 52

On Error GoTo err_chk
dataFound = True
Set cellrange1 = Range("M8:M47").Cells.SpecialCells(xlCellTypeConstants)
If Range("N1").Value = "1530" Then
    Range("N1").Value = "0600"
    Range("L1").Value = Range("L1").Value + 1
    If dataFound Then
        For Each cel In cellrange1
            cel.Value = cel.Value + 1
        Next
    End If
Else
    Range("N1").Value = "1530"
End If

dataFound = True
Sheets("Status Sheet 3502").Select
Set cellrange2 = Range("M8:M44").Cells.SpecialCells(xlCellTypeConstants)
If Range("N1").Value = "0600" Then
    If dataFound Then
        For Each cel1 In cellrange2
            cel1.Value = cel1.Value + 1
        Next
    End If
End If

On Error GoTo 0

Exit Sub

'   Error handling code
err_chk:
'   Ignore if no data found
    If Err.Number = 1004 Then
        dataFound = False
        Err.Clear
        Resume Next
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If

End Sub
 
Upvote 0
Thanks guys both options worked a treat but went with Fluffs! ??
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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