Consulting

Results 1 to 3 of 3

Thread: Using a userform with a for loop

  1. #1
    VBAX Regular
    Joined
    Jan 2016
    Posts
    22
    Location

    Using a userform with a for loop

    Hello,

    I have the following for loop in my workbook:

    Sub IND_External_Find_NAs()
    
    'finds N/A's in column U of IND-External tab, if found, adds item# to Oct 2012 PL tab and asks user for Product Line and Type information
    
    
        Dim CellsToProcess As Range
        Dim sht as Worksheet
    
        Set sht = Sheets("IND-External")
        
    'Delete any extra formulas entered after last row of data in column J of IND-External tab
          lastrow = sht.Cells(Rows.Count, 10).End(xlUp).Row + 1
            sht.Range("L" & lastrow & ":U" & lastrow).Select
                Range(Selection, Selection.End(xlDown)).Select
                    Selection.ClearContents
        
    'Loops through each N/A# found in column U, searches Item list, adds if not found and gets product line and type from user input
        With Sheets("Oct_2012_Item_PL")
            On Error Resume Next
            Set CellsToProcess = sht.Range("U:U").SpecialCells(xlCellTypeFormulas, 16)
            On Error GoTo 0
            If Not CellsToProcess Is Nothing Then
                For Each cll In CellsToProcess.Cells
                    zzz = Application.Match(sht.Cells(cll.Row, "F").Value, .Range("A:A"), 0)
                    If IsError(zzz) Then
                        Set lastitem = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
                        lastitem.Value = sht.Cells(cll.Row, "F").Value
                        lastitem.Offset(0, 1).Value = sht.Cells(cll.Row, "G").Value
                        lastitem.Offset(, 4).Value = "'" & InputBox("Please enter Product Line # for item: " & lastitem.Value)
                        lastitem.Offset(, 6).Value = "'" & InputBox("Please enter Product Type # for item: " & lastitem.Value)
                        lastitem.Offset(, 15).Value = "'" & lastitem.Offset(, 4).Value
                        lastitem.Offset(, 16).Value = "'" & lastitem.Offset(, 6).Value
                    End If
                Next cll
            End If
        End With
    End Sub
    This code loops through a column on one sheet row by row looking for N/As in a vlookup. When one is found, it takes the corresponding item# and description and adds it to the next blank row of the product line sheet. It then prompts the user to enter the product line and product type via input boxes which are also added to the product line sheet. What I would like to do, is replace these two input boxes with a userform. I would like the userform to populate with the item# (lastitem.value) and then have two boxes for the user to input the necessary information. When they press next, I would like the for loop to continue on to the next N/A and repeat the process until there are none left. I am new to userforms and am not sure how to incorporate this into the code above.

    Here is a ss of the form:
    userform2.jpg

    Any tips or help would be greatly appreciated!

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Reverse the paradigm. . . instead of a UserForm inside a loop, consider a loop inside a UserForm.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Don't think of it as a user form, think of it as a custom InputBox and write it to return a comma delimited string of the two values.

    The attached has this in the user form code module
    ' in userform code module
    
    Public Function LineAndType(ItemNumber As String) As String
        With Me
            .txtItem = ItemNumber
            .txtItem.Locked = True
            .butOK.Enabled = False
            .Show
        End With
        With UserForm1
            If .Tag = "OK" Then
                LineAndType = .txtLine & "," & .txtType
            End If
        End With
        UnLoad Userform1
    End Function
    
    Private Sub butCancel_Click()
        Unload Me
    End Sub
    
    Private Sub butOK_Click()
        Me.Tag = "OK"
        Me.Hide
    End Sub
    
    Private Sub txtLine_Change()
        Me.butOK.Enabled = ((Me.txtLine <> vbNullString) And (Me.txtType <> vbNullString))
    End Sub
    
    Private Sub txtType_Change()
        Me.butOK.Enabled = ((Me.txtLine <> vbNullString) And (Me.txtType <> vbNullString))
    End Sub
    And this behind the demo button.
    Sub test()
        Dim userValues As String
        
        userValues = UserForm1.LineAndType(1234)
        
        If userValues = vbNullString Then
            MsgBox "User canceled"
        Else
            MsgBox "User entered " & Split(userValues, ",")(0) & " and " & Split(userValues, ",")(1)
        End If
    End Sub
    Attached Files Attached Files

Posting Permissions

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