Solved

Excel VBA - Coding a Msgbox and GoTO Exit Statement

Posted on 2014-01-20
2
473 Views
Last Modified: 2014-01-20
Here is the code that is not working:
'Sub CommandButton1_Click()
'    If IsEmpty(Range("D3")) then  msgbox("error") GoTo Ex  end if
'    Range("A3:I3").Copy
'    Range("A6:I6").Insert Shift:=xlDown
'    Range("D3:I3").ClearContents
'Ex:
'End Sub

What I want to do is verify that the user has entered data before performing the Range commands.  If the cell D3 is blank I wish to display a Msgbox warning and then Exit the routine before the Range commands are processed.  Thought I could accomplish this with the Msgbox and the GoTo but something is wrong with syntax.  Appreciate any suggestions.
0
Comment
Question by:dlpotlatch
2 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 250 total points
ID: 39795814
You might try revising your code like shown in the snippet below. I put the Range commands in an Else block of the If statement. In so doing, I could eliminate the GoTo statement.
Sub CommandButton1_Click()
If Range("D3").Value = "" Then
    MsgBox "error"
Else
    Range("A3:I3").Copy
    Range("A6:I6").Insert Shift:=xlDown
    Range("D3:I3").ClearContents
End If
End Sub

Open in new window

0
 
LVL 14

Accepted Solution

by:
Farzad Akbarnejad earned 250 total points
ID: 39795905
Hi,
And if you want to keep your own code separate statement with colon ( : ) on if - end if   block as follow:
Sub CommandButton1_Click()
    If IsEmpty(Range("D3")) Then MsgBox ("error"): GoTo Ex: End If
    Range("A3:I3").Copy
    Range("A6:I6").Insert Shift:=xlDown
    Range("D3:I3").ClearContents
Ex:
End Sub

Open in new window

-FA
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question