+ Reply to Thread
Results 1 to 15 of 15

loop controls in userform except one textbox

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2016
    Posts
    12

    loop controls in userform except one textbox

    Hello there,

    I am using a userform where i have my textboxes, comboboxes with a commandbutton.
    When i click on that commandbutton, i would like to reset all the textboxes except one of them.
    Currently i am using the following code i got from the internet. But how can I have one of my textboxes unchanged on the userform?

    Private Sub cmdAdd_Click()
    
    Dim cCont As Control
    
            For Each cCont In Me.Controls
    
                If TypeName(cCont) = "ComboBox" Then
    
                cCont.Value = ""
    
                End If
                
                If TypeName(cCont) = "TextBox" Then
                
                cCont.Value = ""
                
                End If
    
                If TypeName(cCont) = "Label" Then
    
                cCont.Value = ""
    
                End If
                
            Next cCont
    
    End Sub
    Thanks

    gkisacik
    Last edited by gkisacik; 03-28-2010 at 03:18 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,043

    Re: loop controls in userform except one textbox

    Hi gkisacik,

    please use CODE tag around your code.

    Go to post -> Edit -> Go advanced -> select your code and use # sign around it
    Never use Merged Cells in Excel

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: loop controls in userform except one textbox

    Use the TextBox Tag Property, give the TextBox to exclude a value then this code will work
    Option Explicit
    
    Private Sub cmdAdd_Click()
    
        Dim cCont As msforms.Control
    
        For Each cCont In Me.Controls
            With cCont
                Select Case TypeName(cCont)
                Case "ComboBox": .Value = ""
                Case TypeName(cCont) = "TextBox"
                    If .Tag <> "" Then .Value = ""
                Case "Label": .Caption = ""
                End Select
            End With
        Next cCont
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    03-17-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2016
    Posts
    12

    Re: loop controls in userform except one textbox

    Hello RoyUk,

    Not sure it works that way.
    To make it clear;
    I have textboxes like; txtpercentage1, txtpercentage2, txtpercentage3...
    then i have a textbox like txttrial
    I would like to have txttrial out of that loop to be set to "".

    How can i fix this?

    Thanks
    gkisacik

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: loop controls in userform except one textbox

    Try this in Roys' Code change

    Case TypeName(cCont) = "TextBox"
                    If .Tag <> "" Then .Value = ""
    To

    Case TypeName(cCont) = "TextBox"
                    If .Name <> "txttrial" Then .Value = ""

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: loop controls in userform except one textbox

    Why not try the code,add anything to the Tag property of txttrial, say x. Then run the code

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: loop controls in userform except one textbox

    Apologies Roy.

    I should have first said that your code works perfectly well.

  8. #8
    Registered User
    Join Date
    03-17-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2016
    Posts
    12

    Re: loop controls in userform except one textbox

    Hello Marcol,

    For txttrial it works fine but not for the other textboxes (txtpercentage1, txtpercentage2...)

    Any other suggestion?

    Thanks
    gkisacik

  9. #9
    Registered User
    Join Date
    03-17-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2016
    Posts
    12

    Re: loop controls in userform except one textbox

    sorry that i forgot to mention. even with RoyUK's code the textboxes dont get set to "".

    I am sending you now the full code; Any suggestion is welcome.

    Private Sub cmdAdd_Click()
    Dim i As Integer
    Dim iRow As String
    Dim ws As Worksheet
    Dim wsdata As Worksheet
    
    'check for a part number
    If Trim(Me.cbocode1.Value) = "" Then
      Me.cbocode1.SetFocus
      MsgBox "Please choose a code number", vbExclamation, "Code no missing"
      Exit Sub
    End If
    If txttrial.Value = "1" Then
    Set ws = Worksheets("Trial " & txttrial.Value)
    ws.Activate
    'find first empty row in database
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.txttrial.Value
    ws.Cells(iRow, 2).Value = Me.cbocode1.Value
    ws.Cells(iRow, 3).Value = Me.txtdescription1.Value
    ws.Cells(iRow, 4).Value = Me.txtpercentage1.Value
    ws.Cells(iRow, 5).Value = Me.txtgr1.Value
    
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ws.Cells(iRow, 1).Value = Me.txttrial.Value
    ws.Cells(iRow, 2).Value = Me.cbocode2.Value
    ws.Cells(iRow, 3).Value = Me.txtdescription2.Value
    ws.Cells(iRow, 4).Value = Me.txtpercentage2.Value
    ws.Cells(iRow, 5).Value = Me.txtgr2.Value
    
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ws.Cells(iRow, 1).Value = Me.txttrial.Value
    ws.Cells(iRow, 2).Value = Me.cbocode3.Value
    ws.Cells(iRow, 3).Value = Me.txtdescription3.Value
    ws.Cells(iRow, 4).Value = Me.txtpercentage3.Value
    ws.Cells(iRow, 5).Value = Me.txtgr3.Value
    
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ws.Cells(iRow, 1).Value = Me.txttrial.Value
    ws.Cells(iRow, 2).Value = Me.cbocode4.Value
    ws.Cells(iRow, 3).Value = Me.txtdescription4.Value
    ws.Cells(iRow, 4).Value = Me.txtpercentage4.Value
    ws.Cells(iRow, 5).Value = Me.txtgr4.Value
    
    If cbocode5.Value = "" Then
        
        Dim cCont As msforms.Control
        
        For Each cCont In Me.Controls
            With cCont
                Select Case TypeName(cCont)
                Case "ComboBox": .Value = ""
                Case TypeName(cCont) = "TextBox"
                    If .Tag <> "x" Then .Value = ""
                Case TypeName(cCont) = "Label"
                    If .Name = "lblproduct" Then .Caption = ""
                End Select
            End With
        Next cCont
            Me.txttotalweight.Value = ""
            Me.txttotalpercentage.Value = ""
            Me.cbocode1.SetFocus
            txttrial.Value = txttrial.Value + 1
    Exit Sub

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: loop controls in userform except one textbox

    What are you asking?

    txtpercentage1 = "", txtpercentage2 = "", txtpercentage3 = ""............

    txttrial = "some text"

    If so BOTH variations of the code work.

    Are you wanting to exclude other controls, for instance Labels?

    EDIT:

    Posted before I saw your code.
    Last edited by Marcol; 03-27-2010 at 05:31 PM. Reason: Forgot to refresh browser

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: loop controls in userform except one textbox

    Your code as supplied has two If statements with no End If

    I have no idea where this should be fixed, only you know that

    Change the code block to

        Dim cCont As msforms.Control
        Dim i As Integer
        
        For i = 0 To Me.Controls.Count - 1
            Set cCont = Me.Controls(i)
            With cCont
                Select Case TypeName(cCont)
                    Case "ComboBox": .Value = ""
                    Case "TextBox"
                        If .Tag <> "x" Then .Value = ""
                    Case "Label"
                        If .Name = "lblproduct" Then .Caption = ""
                    End Select
            End With
        Next
    Depending on where your If statements end, you may not need these two lines


            Me.txttotalweight.Value = ""
            Me.txttotalpercentage.Value = ""
    Last edited by Marcol; 03-27-2010 at 05:38 PM. Reason: typos

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: loop controls in userform except one textbox

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!

    The reason that I suggested using the Tag property is that it is easier to maintain, you can add the exception to other TextBoxes or remove it easily this way.

    Also,Select case is much easier to follow.

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: loop controls in userform except one textbox

    I pasted incorrectly in the first post, my code should be
    Private Sub CommandButton1_Click()
     Dim cCont As msforms.Control
    
        For Each cCont In Me.Controls
            With cCont
                Select Case TypeName(cCont)
                Case "ComboBox": .Value = ""
                Case "TextBox"
                    If .Tag = "" Then .Value = Empty
                Case "Label": .Caption = ""
                End Select
            End With
        Next cCont
    End Sub
    I can't really see why you need to clear labels

    Well spotted Marcol
    Last edited by royUK; 03-28-2010 at 04:30 AM.

  14. #14
    Registered User
    Join Date
    03-17-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2016
    Posts
    12

    Re: loop controls in userform except one textbox

    Hey Thanks RoyUK,

    yesterday i could not see it anymore. I wanted to sleep over and start with fresh brains :-).

    But with your last post, the code worked better.

    Thank you very much
    gkisacik

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: loop controls in userform except one textbox

    No problem

+ 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