用户表单:在文本框中保留 运行 总数
Userform: Keep Running Total in Textbox
我希望每次在我的用户表单中更新列表框时更新一个文本框值。我有两个用户表单,用户可以在它们之间来回移动值)。我希望每次值在用户窗体之间来回移动时更新文本框值。这是我下面的代码。
Private Sub btnMoveLeft_Click()
Dim iCtr As Long
For iCtr = 0 To Me.lstAssigned.ListCount - 1
If Me.lstAssigned.Selected(iCtr) = True Then
Me.lstUnassigned.AddItem Me.lstAssigned.List(iCtr)
lookValue = WorksheetFunction.VLookup(Me.lstUnassigned(iCtr), Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value - lookValue
End If
Next iCtr
For iCtr = Me.lstAssigned.ListCount - 1 To 0 Step -1
If Me.lstAssigned.Selected(iCtr) = True Then
Me.lstAssigned.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub btnMoveRight_Click()
Dim iCtr As Long
For iCtr = 0 To Me.lstUnassigned.ListCount - 1
If Me.lstUnassigned.Selected(iCtr) = True Then
Me.lstAssigned.AddItem Me.lstUnassigned.List(iCtr)
lookValue = WorksheetFunction.VLookup(Me.lstAssigned.List(iCtr), Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value + lookValue
End If
Next iCtr
For iCtr = Me.lstUnassigned.ListCount - 1 To 0 Step -1
If Me.lstUnassigned.Selected(iCtr) = True Then
Me.lstUnassigned.RemoveItem iCtr
End If
Next iCtr
End Sub
我目前收到的错误是当它试图获取 lookValue 时,错误显示 "Could not get the List property. Invalid property array index." 任何帮助将不胜感激,非常感谢。
在很多情况下,最好使用中间变量,而不是重复执行相同的操作。在这种情况下,您的问题是检索当前选定列表项的 Me.lstAssigned.List(iCtr)
操作。通过使用中间变量,我能够让您的代码正常工作,并且无需第二个循环即可从该列表框中删除该项目。
Private Sub btnMoveLeft_Click()
Dim iCtr As Long
Dim selectedValue As Variant
For iCtr = 0 To Me.lstAssigned.ListCount - 1
If Me.lstAssigned.Selected(iCtr) = True Then
selectedValue = Me.lstAssigned.List(iCtr)
Me.lstUnassigned.AddItem selectedValue
lookValue = WorksheetFunction.VLookup(selectedValue, _
Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value - lookValue
Me.lstAssigned.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub btnMoveRight_Click()
Dim iCtr As Long
Dim selectedValue As Variant
For iCtr = 0 To Me.lstUnassigned.ListCount - 1
If Me.lstUnassigned.Selected(iCtr) = True Then
selectedValue = Me.lstUnassigned.List(iCtr)
Me.lstAssigned.AddItem selectedValue
lookValue = WorksheetFunction.VLookup(selectedValue, _
Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value + lookValue
Me.lstUnassigned.RemoveItem iCtr
End If
Next iCtr
End Sub
我希望每次在我的用户表单中更新列表框时更新一个文本框值。我有两个用户表单,用户可以在它们之间来回移动值)。我希望每次值在用户窗体之间来回移动时更新文本框值。这是我下面的代码。
Private Sub btnMoveLeft_Click()
Dim iCtr As Long
For iCtr = 0 To Me.lstAssigned.ListCount - 1
If Me.lstAssigned.Selected(iCtr) = True Then
Me.lstUnassigned.AddItem Me.lstAssigned.List(iCtr)
lookValue = WorksheetFunction.VLookup(Me.lstUnassigned(iCtr), Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value - lookValue
End If
Next iCtr
For iCtr = Me.lstAssigned.ListCount - 1 To 0 Step -1
If Me.lstAssigned.Selected(iCtr) = True Then
Me.lstAssigned.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub btnMoveRight_Click()
Dim iCtr As Long
For iCtr = 0 To Me.lstUnassigned.ListCount - 1
If Me.lstUnassigned.Selected(iCtr) = True Then
Me.lstAssigned.AddItem Me.lstUnassigned.List(iCtr)
lookValue = WorksheetFunction.VLookup(Me.lstAssigned.List(iCtr), Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value + lookValue
End If
Next iCtr
For iCtr = Me.lstUnassigned.ListCount - 1 To 0 Step -1
If Me.lstUnassigned.Selected(iCtr) = True Then
Me.lstUnassigned.RemoveItem iCtr
End If
Next iCtr
End Sub
我目前收到的错误是当它试图获取 lookValue 时,错误显示 "Could not get the List property. Invalid property array index." 任何帮助将不胜感激,非常感谢。
在很多情况下,最好使用中间变量,而不是重复执行相同的操作。在这种情况下,您的问题是检索当前选定列表项的 Me.lstAssigned.List(iCtr)
操作。通过使用中间变量,我能够让您的代码正常工作,并且无需第二个循环即可从该列表框中删除该项目。
Private Sub btnMoveLeft_Click()
Dim iCtr As Long
Dim selectedValue As Variant
For iCtr = 0 To Me.lstAssigned.ListCount - 1
If Me.lstAssigned.Selected(iCtr) = True Then
selectedValue = Me.lstAssigned.List(iCtr)
Me.lstUnassigned.AddItem selectedValue
lookValue = WorksheetFunction.VLookup(selectedValue, _
Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value - lookValue
Me.lstAssigned.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub btnMoveRight_Click()
Dim iCtr As Long
Dim selectedValue As Variant
For iCtr = 0 To Me.lstUnassigned.ListCount - 1
If Me.lstUnassigned.Selected(iCtr) = True Then
selectedValue = Me.lstUnassigned.List(iCtr)
Me.lstAssigned.AddItem selectedValue
lookValue = WorksheetFunction.VLookup(selectedValue, _
Sheets("Pivot").Range("A:J"), 10, False)
txtAC.Value = txtAC.Value + lookValue
Me.lstUnassigned.RemoveItem iCtr
End If
Next iCtr
End Sub