Message box if nil in code

cfer

Well-known Member
Joined
Jul 29, 2002
Messages
560
Hi,

I have the following code,

if there is a zero in the range M1, I want to be able to halt the code, and a Pop up message, There is no data for this range.

Sub CopyFormula()

Worksheets("Finance").Select
NumberOfCopies = Sheets("Details").Range("M1").Value
With Worksheets("Finance").Range("C8:M8")
.Copy .Offset(1).Resize(NumberOfCopies, 1)
End With

Any help appreciatted.

Cfer

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi. Try

Code:
Sub CopyFormula()
If Sheets("Details").Range("M1").Value = 0 Then
    MsgBox "No data"
    Exit Sub
End If
Worksheets("Finance").Select
NumberOfCopies = Sheets("Details").Range("M1").Value
With Worksheets("Finance").Range("C8:M8")
.Copy .Offset(1).Resize(NumberOfCopies, 1)
End With
 
Upvote 0
Another possible way to go ..
Code:
Sub CopyFormula()
    NumberOfCopies = Sheets("Details").Range("M1").Value
    Select Case NumberOfCopies
        Case 0
            MsgBox "No data"
        Case Is > 0
            With Worksheets("Finance").Range("C8:M8")
                .Copy .Offset(1).Resize(NumberOfCopies)
            End With
    End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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