这是填充列表框的代码,从TextBox中获取大部分数据。
iListCount被声明为全局。由于我不知道的原因,列表框仅填充第一行,除非按此方式执行,否则不会采用后续行。
Private Sub cmdAddFraudCharges_Click()
With Me.lstFCHRGS '<- ListBox
.ColumnCount = 6
.ColumnWidths = "60;65;50;26;35;70"
.AddItem
.List(iListCount, 0) = dtpAddDt |<--DateTimePicker|error occurs here
.List(iListCount, 1) = txtAddAmt |<--Continues till last column
iListCount = iListCount + 1
End With
End Sub
这是我用来同时删除列表框项和工作表条目的代码。
Private Sub cmdDelChrgs_Click()
Dim x As Integer
Dim y As Integer
Dim t As Integer
Dim rngTarget As Range
Set rngTarget = wksFraud.Range("A1").End(xlDown)
Set wksFraud = Application.ThisWorkbook.Worksheets("Fraud")
For iListCount = lstFCHRGS.ListCount - 1 To 0 Step -1
x = lstFCHRGS.ListCount
If lstFCHRGS.Selected(iListCount) Then
y = iListCount
t = (x - y) - 1
With rngTarget
Set rngTarget = rngTarget.Offset(-t, 0)
rngTarget.EntireRow.Delete
End With
lstFCHRGS.RemoveItem (iListCount)
End If
Next iListCount
End Sub
答案 0 :(得分:1)
将5个项目添加到列表后iListCount将等于4. due 到这一行iListCount = iListCount + 1它现在将等于5。
Private Sub cmdDelChrgs_Click() Dim x As Integer Dim y As Integer Dim t As Integer
Dim rngTarget As Range
Set rngTarget = wksFraud.Range("A1").End(xlDown)
Set wksFraud = Application.ThisWorkbook.Worksheets("Fraud")
'For iListCount = lstFCHRGS.ListCount - 1 To 0 Step -1|<-- changed this with
For iListCount = LBound(lstFCHRGS.List) To UBound(lstFCHRGS.List)
此时iListCount = 0 因为它循环iListCount将具有值5
If lstFCHRGS.Selected(iListCount) Then
x = lstFCHRGS.ListCount
y = iListCount
t = (x - y) - 1
With rngTarget
Set rngTarget = rngTarget.Offset(-t, 0)
rngTarget.EntireRow.Delete
End With
lstFCHRGS.RemoveItem (iListCount)
End If
Next iListCount
删除项目后iListCount仍为5,可能是错误的原因
iListCount = iListCount - 1|<-- Add after next to restores the value of iListCount