简体   繁体   中英

VBA UserForm in Excel--mismatched rows on spreadsheet data entry

I have a userform created via VBA that is supposed to populate rows on an Excel sheet. It does, but they're one off:

Name   |   Race   |   Agency
           Black
Joe        Asian      B
           White
Joanne                C

Joe's races are black and Asian, at agency B; Joanne's is white, and she's at agency C. Somehow, the entries are staggered.

Name is a textbox, race and agency are listboxes, with race as a multiselect and agency as a single select.

Here's my code:

Private Sub CommandButton1_Click()
Dim j As Long
Dim i As Integer
With ListBox2
ReDim arr(.ListCount - 1)
    For i = 0 To .ListCount - 1
        If .Selected(i) = True Then
        .Selected(i) = False
        arr(j) = .List(i)
        j = j + i
        End If
    Next i
End With
ReDim Preserve arr(j)
With ActiveSheet
.Range("B" & .Rows.Count).End(xlUp). _
Offset(1, 0).Resize(j + 1, 1).Value = Application.Transpose(arr)
End With
i = 1
While ThisWorkbook.Worksheets("Sheet1").Range("B" & i).Value <> ""
i = i + 1
Wend
ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Value = TextBox1.Value
ThisWorkbook.Worksheets("Sheet1").Range("C" & i).Value = ListBox1.Value
End Sub
Private Sub CommandButton2_Click()
Dim ctl As MSForms.Control
For Each ctl In Me.Controls
    Select Case TypeName(ctl)
        Case "TextBox"
            ctl.Text = ""
        Case "CheckBox", "OptionButton", "ToggleButton"
            ctl.Value = False
        Case "ComboBox", "ListBox"
            ctl.ListIndex = -1
    End Select
Next ctl
End Sub
Sub UserForm_Initialize()
ListBox1.List = Array("A", "B", "C")
With ListBox2
  .Clear
  .AddItem "White"
  .AddItem "Black"
  .AddItem "Asian"
  .AddItem "Am Indian/Al Native"
  .AddItem "Native Hawaiian/Pac Islander"
  .AddItem "Other"
End With
End Sub

I would love any ideas ya'll have on how to fix that! Ideally, it would come out in one of the following ways:

Name   |   Race   |   Agency
Joe        Black      B
           Asian      B
Joanne     White      C

or

Name   |   Race          |   Agency
Joe        Black, Asian      B
Joanne     White             C

or

Name   |   Race   |   Agency
Joe        Black      B
Joe        Asian      B
Joanne     White      C

(I prefer the second, but any would work.)

If I understand the code appropriately, the refactored CommandButton1_Click procedure below should produce the preferred result for you.

Private Sub CommandButton1_Click()

Dim j As Long
Dim i As Integer

'load races into array
With ListBox2

    ReDim arr(.ListCount - 1)

    For i = 0 To .ListCount - 1

        If .Selected(i) = True Then
            .Selected(i) = False
            arr(j) = .List(i)
            j = j + i
        End If

    Next i

End With

ReDim Preserve arr(j)

'build "," separated string of races
For i = LBound(arr) To UBound(arr)

    Dim sRace As String
    sRace = sRace & "," & arr(i)

Next
sRace = Mid(sRace, 2) 'to remove first comma

'place info on next available line in sheet.
With ThisWorkbook.Worksheets("Sheet1")

    Dim lRow As Long
    lRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row

    .Range("A" & lRow).Value = TextBox1.Value
    .Range("B" & lRow).Value = sRace
    .Range("C" & lRow).Value = ListBox1.Value

End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM