+ Reply to Thread
Results 1 to 3 of 3

Trouble with if else then macro

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2015
    Location
    Auckland
    MS-Off Ver
    2013
    Posts
    1

    Trouble with if else then macro

    Hi all,

    I am trying to code a macro that first checks cells B4,C4,D4 and E4 to check they have been filled in. If they haven't a msgbox should pop up prompting to fill in required cells. If the cells are filled in the various functions in the recorded macro should run.

    Note, All relevant columns have data validation applied.

    I have tried several methods. IsNull() with OR for the various cells, B4=""..., B4=0.. etc. but I can't seem to get it to work correctly.

    In the code below, if the cells are empty the msgbox comes up however even when the cells are completed the msgbox comes up and the macro does not run the Else portion of the code.
    If B4 = "" Then
       MsgBox ("Please enter data in all cells")
       ElseIf C4 = "" Then
       MsgBox ("Please enter data in all cells")
        ElseIf D4 = "" Then
       MsgBox ("Please enter data in all cells")
       ElseIf E4 = "" Then
       MsgBox ("Please enter data in all cells")
       Else
        ActiveSheet.Unprotect
        Rows("4:4").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("B5:E5").Select
        Range("E5").Activate
        ActiveSheet.Shapes.Range(Array("Rectangle 7")).Select
        Range("B5:E5").Select
        Range("E5").Activate
        Selection.Locked = True
        Selection.FormulaHidden = False
        Range("B2:E4").Select
        Range("E4").Activate
        Selection.Locked = False
        Selection.FormulaHidden = False
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
            :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
            AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    End If
    End Sub
    Last edited by Leith Ross; 04-08-2015 at 10:13 PM.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Trouble with if else then macro

    Hello,

    Here's a suggestion (with a bit of code clean up).
    (warning : not tested !)

    Sub test()
    
        With ActiveSheet
            If .Range("B4").Value = "" Or .Range("C4").Value = "" Or _
               .Range("D4").Value = "" Or .Range("E4").Value = "" Then
    
                MsgBox "Please enter data in all cells !", vbExclamation
                Exit Sub
            Else
    
                .Unprotect
                .Rows("4:4").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                .Shapes.Range(Array("Rectangle 7")).Select
                With .Range("E4:E5")
                    .Locked = True
                    .FormulaHidden = False
                End With
                .Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
                         False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                         AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, _
                         AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
                         AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    
            End If
        End With
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,565

    Re: Trouble with if else then macro

    Or:
    If WorksheetFunction.CountA(Range("B4:E4")) < 4 Then MsgBox "Please fill the appropiate cells first.": Exit Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. macro for turning columns into rows
    By Shanehickey2497 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-18-2013, 01:08 AM
  2. Macro Trouble
    By tonychopra194 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-30-2012, 02:57 AM
  3. Trouble with If-Then-Else in a VBA Macro
    By garden_gnome in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-07-2012, 04:17 PM
  4. Replies: 0
    Last Post: 08-26-2010, 11:49 AM
  5. MACRO TROUBLE
    By HELP in forum Excel General
    Replies: 2
    Last Post: 06-07-2006, 11:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1