Solved

Excel VBA - Coding a Msgbox and GoTO Exit Statement

Posted on 2014-01-20
2
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

739 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