以下是问题: 1)我通过VBA代码强制在列表框中选择某些项 2)打开用户窗体,然后选择或取消选择某些项目。 (一切似乎在列表框中都很好) 3)写出选择的项目。 如果我只选择一些新项目,一切正常。如果我取消选择了我一开始就强制选择的选定项目,则在输出中仍将其选中。
With Sheets("ID_Mitarbeiter").Range("A2:A1048576")
Set c = .Find(What:=TextBox_ID, lookat:=xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
ListBox_Mitarbeiter.Selected(Sheets("ID_Mitarbeiter").Cells(c.Row, 2) - 1) = True
Set c = .FindNext(c)
If c Is Nothing Then
GoTo DoneFinding
End If
Loop While c.Address <> firstAddress
End If
DoneFinding: 结尾为
答案 0 :(得分:0)
Private Sub Butto_Change_Click()
Dim ind_pers As Integer
With ListBox_Mitarbeiter
If .ListCount > 0 Then
ind_pers = 0
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Cells(ind_pers, 1).Value = 1
Cells(ind_pers, 2).Value = i + 1
ind_pers = ind_pers + 1
Else
End If
Next i
Else
End If
End With
End Sub
Private Sub ComboBox1_Change()
TextBox_IndexStart.Value = Sheets("ID_Mitarbeiter").Range("A2:A1048576").Find(What:=TextBox_ID.Value, lookat:=xlWhole).Row
With Sheets("ID_Mitarbeiter").Range("A2:A1048576")
Set c = .Find(What:=TextBox_ID, lookat:=xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
ListBox_Mitarbeiter.Selected(Sheets("ID_Mitarbeiter").Cells(c.Row, 2) - 1) = True
Set c = .FindNext(c)
If c Is Nothing Then
GoTo DoneFinding
End If
Loop While c.Address <> firstAddress
End If
DoneFinding:
End With
End If
End Sub
Private Sub UserForm_Initialize()
Dim last As Integer
Dim cnt As Integer
last = Sheets("Mitarbeiter").Cells(Rows.Count, 1).End(xlUp).Row
For cnt = 2 To last
With ListBox_Mitarbeiter
.AddItem Sheets("Mitarbeiter").Cells(cnt, 2).Value & " " & Sheets("Mitarbeiter").Cells(cnt, 3).Value
End With
Next cnt
End Sub
此代码可以正常工作。但是当我在用户界面中取消选择项目时,它仍然会首先写出所有选择的项目。