插入选定的行(多选),从列表框到 sheet,我想禁用列表框中的那些项目以避免在 sheet 中重复它们
insert selected rows(multiselect), from listbox to sheet, i want to diable those items in the listbox to avoid duplicating them in the sheet
Private Sub cmdB2_Click()
Dim i, x As Integer
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) = True And Me.ListBox1.List(i, 1) <> "" Then
Call wS_bill
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Me.ListBox1.List(i, 0)
For x = 0 To 2
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(0, x) = Me.ListBox1.List(i, x)
Me.ListBox1.List(i, x) = "" **-->error pops here**
Me.ListBox1.Selected(i) = False
Next x
Else
Me.ListBox1.Selected(i) = False
End If
Next i
End Sub
请试试这个方法:
For x = 0 To 2
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(0, x) = Me.ListBox1.list(i, x)
Next x
Me.ListBox1.RemoveItem (i) 'or
'Me.ListBox1.RemoveItem (Me.ListBox1.ListIndex)
而不是这个:
For x = 0 To 2
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(0, x) = Me.ListBox1.List(i, x)
Me.ListBox1.List(i, x) = "" '**-->error pops here**
Me.ListBox1.Selected(i) = False
Next x
上述解决方案不适用于通过 RowSource
.
链接到范围的列表框
所以,还有一些问题需要适配。一、列表框加载方式:
在您的表单模块之上创建一个变量(在声明区域,在 Option Explicit
之后):
Private arr_DispIt As Variant
'我保留了一个与您提供的范围名称相似的名称...
在Sub Search_R()
过程中,替换(或注释代码行):
Range(Selection, Selection.End(xlDown).End(xlToRight)).Select
和
`arr_DispIt = Range(Selection, Selection.End(xlDown).End(xlToRight)).Value`
和评论 Selection.Name = "Disp_itm"
.
我们不使用命名范围。我们将改用 arr_DispIt
,这将允许删除列表框行。
在 Private Sub cmdB1_Click()
中替换(或注释)行:
ListBox1.RowSource = "Disp_itm"
与:
`Me.ListBox1.List = arr_DispIt`
终于用上了我最初的命题,不过是这样的:
Me.ListBox1.RemoveItem i: Exit For
Private Sub cmdB2_Click()
Dim i, x As Integer
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) = True And Me.ListBox1.List(i, 1) <> "" Then
Call wS_bill
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Me.ListBox1.List(i, 0)
For x = 0 To 2
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(0, x) = Me.ListBox1.List(i, x)
Me.ListBox1.List(i, x) = "" **-->error pops here**
Me.ListBox1.Selected(i) = False
Next x
Else
Me.ListBox1.Selected(i) = False
End If
Next i
End Sub
请试试这个方法:
For x = 0 To 2
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(0, x) = Me.ListBox1.list(i, x)
Next x
Me.ListBox1.RemoveItem (i) 'or
'Me.ListBox1.RemoveItem (Me.ListBox1.ListIndex)
而不是这个:
For x = 0 To 2
Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(0, x) = Me.ListBox1.List(i, x)
Me.ListBox1.List(i, x) = "" '**-->error pops here**
Me.ListBox1.Selected(i) = False
Next x
上述解决方案不适用于通过 RowSource
.
所以,还有一些问题需要适配。一、列表框加载方式:
在您的表单模块之上创建一个变量(在声明区域,在
Option Explicit
之后):Private arr_DispIt As Variant
'我保留了一个与您提供的范围名称相似的名称...在
Sub Search_R()
过程中,替换(或注释代码行):Range(Selection, Selection.End(xlDown).End(xlToRight)).Select
和
`arr_DispIt = Range(Selection, Selection.End(xlDown).End(xlToRight)).Value`
和评论 Selection.Name = "Disp_itm"
.
我们不使用命名范围。我们将改用 arr_DispIt
,这将允许删除列表框行。
在
Private Sub cmdB1_Click()
中替换(或注释)行:ListBox1.RowSource = "Disp_itm"
与:
`Me.ListBox1.List = arr_DispIt`
终于用上了我最初的命题,不过是这样的:
Me.ListBox1.RemoveItem i: Exit For