Solved

Excel VBA - Coding a Msgbox and GoTO Exit Statement

Posted on 2014-01-20
2
438 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 80

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now