Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA - Coding a Msgbox and GoTO Exit Statement

Posted on 2014-01-20
2
Medium Priority
?
547 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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

916 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