+ Reply to Thread
Results 1 to 4 of 4

Else If Confusion in VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Else If Confusion in VBA

    Hi folks

    Yet again I show my ignorance. I've got a combo box with the months of the year linked to cell A30.

    Why, in the code below does the code work perfectly for August, but nothing happens for May:

    
    Private Sub ComboBox1_Change()
    
        If Range("A30") = "August" Then
           
        
        Columns("I:I").Select
        Selection.Insert Shift:=xlToRight
        Range("I4").Select
        ActiveCell.FormulaR1C1 = "August"
        
        Else
        
        If Range("A30") = May Then
        
        Columns("I:I").Select
        Selection.Insert Shift:=xlToRight
        Range("I4").Select
        ActiveCell.FormulaR1C1 = "May"
            
        End If
        End If
        
            
    End Sub
    Thanks

    Steve

  2. #2
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Else If Confusion in VBA

    Don't worry! I've noticed the blunder!

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,692

    Re: Else If Confusion in VBA

    Probably because you're missing quotes around May and it is being seen as an undefined variable with null content.

    Maybe:

    Private Sub ComboBox1_Change()
        If Range("A30") = "August" Then
            Columns("I:I").Insert Shift:=xlToRight
            Range("I4").FormulaR1C1 = "August"
        End If
        If Range("A30") = "May" Then
            Columns("I:I").Insert Shift:=xlToRight
            Range("I4").FormulaR1C1 = "May"
        End If
    End Sub

    Regards
    Last edited by TMS; 06-27-2011 at 10:27 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Else If Confusion in VBA

    Hi,
    Try this
    Private Sub ComboBox1_Change()
    
        If Range("A30") = "August" Then
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight
            Range("I4").Select
            ActiveCell.FormulaR1C1 = "August"
        End If
       
        If Range("A30") = "May" Then
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight
            Range("I4").Select
            ActiveCell.FormulaR1C1 = "May"
        End If
            
    End Sub
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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