我正在为我的高年级项目工作,我必须处理用户界面的Activex控件。为了删除列表框中的单个项目,我在下面编写了代码。
调试代码时,代码的最后部分出现“自动化错误404”:
With Worksheets("Solve").OLEObjects("ListBox1")
.ListFillRange = .ListFillRange
End With
这是我的代码:
Dim i As Integer
Dim lastrow As Integer
lastrow = Sheets("SrData").Cells(Sheets("SrData").Rows.Count, "A").End(xlUp).Row
If MsgBox("Are you sure you want to delete this Sales Representative?", vbYesNo + vbQuestion, "Delete Sales Representative") = vbYes Then
For i = 2 To lastrow
sheetcontrol = Sheets("SrData").Cells(i, 1)
listcontrol = Sheets("Solve").ListBox1.List(Sheets("Solve").ListBox1.ListIndex)
If sheetcontrol = listcontrol Then
Sheets("SrData").Activate
ActiveWorkbook.Names("SrDat").Delete
Sheets("SrData").Rows(i).Select
Selection.Delete
Sheets("SrData").Range(Sheets("SrData").Cells(2, 1), Sheets("SrData").Cells(lastrow - 1, 3)).Select
ActiveWorkbook.Names.Add Name:="SrDat", RefersTo:=Selection
End If
Next i
End If
With Worksheets("Solve").OLEObjects("ListBox1")
.ListFillRange = .ListFillRange
End With
答案 0 :(得分:0)
我会尝试使用类似这样的东西:
For i = ListBox1.ListCount - 1 To 0 Step -1
If ListBox1.Selected(i) Then
ListBox1.RemoveItem i
End If
Next i
编辑:然后您可以使用代码删除“解决”工作表中的行,并按如下所示更新列表范围:
Private sub userform_initialize()
With Worksheets("Solve").Range("A2:A" & lastrow) '<-- change with your range
Me.ListBox1.List = Application.Transpose(.Cells)
Set listRng = .Cells
End With
End Sub
答案 1 :(得分:0)
我找到了解决我的问题的方法,这里是我编辑的代码。
Set s = Worksheets("Solve")
Set sd = Worksheets("SrData")
Dim lastrow As Integer
Dim target As String
Dim sheetcontrol As String
Application.ScreenUpdating = False
target = s.ListBox1.List(s.ListBox1.ListIndex)
lastrow = sd.Cells(sd.Rows.Count, "A").End(xlUp).Row
If IsNull(ListBox1) Then
MsgBox "You haven't selected an Sales Representative. Please Select one."
Else
If MsgBox("Are you sure you want to delete this Sales Representative?", vbYesNo + vbQuestion, "Delete Sales Representative") = vbYes Then
For i = 2 To lastrow
sheetcontrol = sd.Cells(i, 1)
If sheetcontrol = target Then
s.ListBox1.ListIndex = -1
sd.Rows(i).Delete
sd.Activate
ActiveWorkbook.Names("SrDat").Delete
Range("'SrData'!$A$2:$C$" & lastrow - 1).Name = "SrDat"
s.Activate
End If
Next i
End If
End If
refSr
Application.ScreenUpdating = True
在refSr部分中,我正在
With Worksheets("Solve").OLEObjects("ListBox1")
.ListFillRange = .ListFillRange
End With