標題:
2個條件的VBA
[打印本頁]
作者:
man65boy
時間:
2012-5-31 23:53
標題:
2個條件的VBA
如何在表單內輸入總表C欄"客戶名稱"裡的關鍵字,然後在ListBox1裡出現有關關鍵字裡的姓名,點選後,在ListBox2裡會出現符合姓名的歷史資料日期(總表B欄),點選後,按確認,轉寫符合這2項的資料,在報價列印的B7:G30
依序往下排列。請教先進老師們不另指教!
[attach]11207[/attach]
作者:
GBKEE
時間:
2012-6-1 09:03
回復
1#
man65boy
ListBox1 更換為 ComboBox1
ListBox2 更換為 ComboBox2
Dim xlSh As Worksheet
Private Sub UserForm_Initialize()
Set xlSh = Sheets("總表")
TextBox1_Change '
End Sub
Private Sub CommandButton1_Click()
Dim Rng As Range, xi As Integer
If ComboBox1.ListIndex > -1 And ComboBox2.ListIndex > -1 Then
With xlSh
xi = 2
Do While .Cells(xi, "C") <> ""
If .Cells(xi, "B").Text = ComboBox2 And .Cells(xi, "C") = ComboBox1 Then
If Rng Is Nothing Then
Set Rng = .Range(.Cells(xi, "d"), .Cells(xi, "i"))
Else
Set Rng = Union(Rng, .Range(.Cells(xi, "d"), .Cells(xi, "i")))
End If
End If
xi = xi + 1
Loop
Rng.Copy
Sheets("報價單列印").Cells(Rows.Count, "b").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End With
Else
MsgBox "客戶名稱 或 日期 ??? "
End If
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
Private Sub TextBox1_Change()
Dim xi As Integer, xlString As String
With xlSh
xi = 2
Do While .Cells(xi, "C") <> ""
If .Cells(xi, "C") Like "*" & TextBox1 & "*" Then
If InStr(xlString, "," & .Cells(xi, "C") & ",") = 0 Then
xlString = xlString & "," & .Cells(xi, "C") & ","
End If
End If
xi = xi + 1
Loop
End With
If xlString = "" Then 'TextBox1的內容找不到
ComboBox1.Clear
ComboBox2.Clear
Exit Sub
End If
With ComboBox1
.List = Split(Mid(xlString, 2, Len(xlString) - 2), ",,")
.Value = .List(0)
End With
End Sub
Private Sub ComboBox1_Change()
Dim xi As Integer, xlString As String
If ComboBox1.ListIndex > -1 Then
'控制項.ListIndex = -1 控制項的 值或選項 不在List內
With xlSh
xi = 2
Do While .Cells(xi, "C") <> ""
If .Cells(xi, "C") = ComboBox1 Then
If InStr(xlString, "," & .Cells(xi, "B") & ",") = 0 Then
xlString = xlString & "," & .Cells(xi, "B") & ","
End If
End If
xi = xi + 1
Loop
End With
With ComboBox2
.List = Split(Mid(xlString, 2, Len(xlString) - 2), ",,")
.Value = .List(0)
End With
ElseIf ComboBox1.ListIndex = -1 Then
ComboBox2.Clear
End If
End Sub
複製代碼
作者:
man65boy
時間:
2012-6-1 20:43
回復
2#
GBKEE
感謝GBKEE大大的回答,查詢的地方,真得很好用,小弟不材再請GBKEE大大修改一下,轉寫前清除B7:G30,在轉寫資料到B7:G30,(不做重複的資料往下排列),動作完成時不要有選取的動作殘留,麻煩請教導要如何修改,
謝謝!!!
作者:
GBKEE
時間:
2012-6-1 21:14
回復
3#
man65boy
新增 CommandButton3
Private Sub CommandButton3_Click()
Sheets("報價單列印").[B7:G30] = ""
End Sub
複製代碼
Rng.Copy
Sheets("報價單列印").Cells(Rows.Count, "b").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
Sheets("報價單列印").[B7].Select '***加上這行程式碼
複製代碼
作者:
man65boy
時間:
2012-6-1 23:26
回復
4#
GBKEE
感謝GBKEE大大費心編寫,小弟得好好拜讀吸收,謝謝!
作者:
billyhsu
時間:
2013-8-30 14:36
學習了,感謝
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)