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
Solved

Excel VBA - Coding a Msgbox and GoTO Exit Statement

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

792 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