+ Reply to Thread
Results 1 to 6 of 6

Using If .. Then to skip a blank combo box in a For...Next

Hybrid View

  1. #1
    Registered User
    Join Date
    01-24-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    31

    Using If .. Then to skip a blank combo box in a For...Next

    The problem is that I need to check combo boxes against each other. I did most of the work, but I don't know what to do about blank boxes. Code is below.

    Dim j As Long
    For j = 1 To 15
        If Me.Controls("cmbVol" & j).Value = BLANK Then
            
        Else
            If j > 1 Then
                Dim q As Long
                For q = 1 To (j - 1)
                    If Me.Controls("cmbVol" & j).Value = Me.Controls("cmbVol" & q).Value Then
                        MsgBox "Yep you did it again"
                        Exit Sub
                    End If
                Next q
                
                Dim w As Long
                For w = (j + 1) To 15
                    If Me.Controls("cmbVol" & j).Value = Me.Controls("cmbVol" & w).Value Then
                        MsgBox "Yep you did it this time"
                        Exit Sub
                    End If
                Next w
            Else
                Dim e As Long
                For e = 2 To 15
                    If Me.Controls("cmbVol" & j).Value = Me.Controls("cmbVol" & e).Value Then
                        MsgBox "Ok Still it"
                        Exit Sub
                    End If
                Next e
        End If
    Next j
    In the first "If" I need something for it to do in order to skip and go on to Next j...such as a Null. But you can't just write null there.

    Or I can reverse the code and use the first if as Not Blank, but I also don't know how to do that.

    Any help is appreciated.


    Extra info:
    Combo boxes are named "cmbVol1" - "cmbVol15"
    They cannot be equal to each other but will not always be filled in. Most of the time there will be some blank ones.
    The goal is to make sure that anything that does have info in it is not the same as something in another box and if so msgBox with message that says so.
    Last edited by Squirrel; 09-09-2011 at 09:12 AM. Reason: solved

  2. #2
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Using If .. Then to skip a blank combo box in a For...Next

    I didn't look too closely at this, but if you're just trying to skip one, you could try this--although it really should be doing that on it's own with the If/Else statement
    Dim j As Long
    For j = 1 To 15
        If Me.Controls("cmbVol" & j).Value = BLANK Then
            GoTo SkipMe
        Else
            If j > 1 Then
                Dim q As Long
                For q = 1 To (j - 1)
                    If Me.Controls("cmbVol" & j).Value = Me.Controls("cmbVol" & q).Value Then
                        MsgBox "Yep you did it again"
                        Exit Sub
                    End If
                Next q
                
                Dim w As Long
                For w = (j + 1) To 15
                    If Me.Controls("cmbVol" & j).Value = Me.Controls("cmbVol" & w).Value Then
                        MsgBox "Yep you did it this time"
                        Exit Sub
                    End If
                Next w
            Else
                Dim e As Long
                For e = 2 To 15
                    If Me.Controls("cmbVol" & j).Value = Me.Controls("cmbVol" & e).Value Then
                        MsgBox "Ok Still it"
                        Exit Sub
                    End If
                Next e
        End If
    SkipMe: Next j
    Last edited by gjlindn; 09-09-2011 at 12:55 AM.
    -Greg If this is helpful, pls click Star icon in lower left corner

  3. #3
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Using If .. Then to skip a blank combo box in a For...Next

    Could also try this:
        Dim ctControl As Control
        Dim ctControl2 As Control
        
        For Each ctControl In Me.Controls
            If Left(ctControl.Name, Len("cmbVol")) = "cmbVol" Then
                For Each ctControl2 In Me.Controls
                    If Left(ctControl2.Name, Len("cmbVol")) = "cmbVol" Then
                        If Len(ctControl.Text) > 0 And ctControl.Text = ctControl2.Text _
                            And Not ctControl Is ctControl2 Then
                            ctControl2.SetFocus
                            MsgBox "duplicate value...select something else"
                            Exit Sub
                        End If
                    End If
                Next
            End If
        Next

  4. #4
    Registered User
    Join Date
    01-24-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Using If .. Then to skip a blank combo box in a For...Next

    I used the second one because it was more concise and it was exactly how it needed to be. Although I don't really understand the code in it. If you had time and put comments in that explained it a little bit that would be great. If not that's ok. Thanks!

  5. #5
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Using If .. Then to skip a blank combo box in a For...Next

    Here it is with some comments. If anything doesn't make sense, feel free to ask. Thanks!
        Dim ctControl As Control
        Dim ctControl2 As Control
        
        'Loop through all controls on the form (Me is the form in this case)
        'the control will be assigned to ctControl
        For Each ctControl In Me.Controls
            'Only look at controls whose names begin with cmbVol
            If Left(ctControl.Name, Len("cmbVol")) = "cmbVol" Then
                'Loop through all controls again but this time the control is assigned
                'to ctControl2
                For Each ctControl2 In Me.Controls
                    'Check again to see if the control name begins with cmbVol
                    If Left(ctControl2.Name, Len("cmbVol")) = "cmbVol" Then
                        'If the control from the first loop is not empty (len(ctControl.text)>0)
                        'and the control form loop 1 has the same text as the control from loop 2
                        'and the control from loop 1 is not the same control from loop 2 then
                        'set the focus to the loop 2 control (ctControl2.SetFocus) and
                        'show a message box explaining the problem...then exit the sub
                        If Len(ctControl.Text) > 0 And ctControl.Text = ctControl2.Text _
                            And Not ctControl Is ctControl2 Then
                            ctControl2.SetFocus
                            MsgBox "duplicate value...select something else"
                            Exit Sub
                        End If
                    End If
                Next
            End If
        Next

  6. #6
    Registered User
    Join Date
    01-24-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Using If .. Then to skip a blank combo box in a For...Next

    great thanks. makes more sense now

+ 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