我尝试创建一个宏,允许我通过使用userform和listbox选择数据库中的某些客户端来报告它们。我目前一直在寻找一种方法来选择某些客户,但是处理该客户下列出的每一行。
我正在使用的脚本存储集合中的所有唯一值。我不确定如何在运行命令按钮中调用集合来处理绑定到所选客户端的所有行,这意味着如果我要在附加图像中选择CLN1和CLN3,我希望行2:6和10:12处理,其余的删除。
有办法做到这一点吗?
以下是我使用的代码:
$db_password = "SamWise" . "$" . "GangiTYloYG";
非常感谢任何帮助 - 谢谢,全部!
答案 0 :(得分:0)
假设在Run_Btn
之后调用了运行命令按钮,那么您可以在用户窗体代码窗格中使用此代码开始:
Private Sub UserForm_Initialize()
Dim i As Long, rowrow As Long, LastNonEmptyRow As Long
Dim Cell As Range
Dim Unique As New Collection
Dim Item As Range
LastNonEmptyRow = ThisWorkbook.Worksheets("myfile").Cells(Rows.Count, 1).End(xlUp).Row
On Error Resume Next
For Each Cell In ThisWorkbook.Worksheets("myfile").Range("A2:A" & LastNonEmptyRow)
Unique.Add Cell, CStr(Cell)
Next Cell
On Error GoTo 0
For Each Item In Unique
ListBox1.AddItem Item
Next Item
End Sub
Private Sub Run_Btn_Click()
Dim deletedClients As String
Dim i As Long
With Me.ListBox1
For i = 0 To .ListCount - 1
If Not .Selected(i) Then deletedClients = deletedClients & .List(i) & " "
Next
End With
If deletedClients <> "" Then ProcessData deletedClients
End Sub
Sub ProcessData(deletedClients As String)
With ThisWorkbook.Worksheets("myfile")
With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
DeleteData .Cells, deletedClients '<--| delete unselected clients records
' your code to "Process" remaining data
End With
End With
End Sub
Sub DeleteData(rng As Range, deletedClients As String)
With rng
.AutoFilter Field:=1, Criteria1:=Split(Trim(deletedClients), " "), Operator:=xlFilterValues
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Parent.AutoFilterMode = False
End With
End Sub