返回列表 上一主題 發帖

VBA 當2個條件一樣時,自動尋找輸入

試試看:
Private Sub CommandButton1_Click()
    Dim Lst As Integer, R As Integer
    Dim Rng As Range, MH
    Dim sh As Worksheet
    Set sh = Sheets("主頁")
    Lst = sh.[A65536].End(xlUp).Row        '取得"主頁"欄A 最下面非空白格的列號
   
    R = 1
    Set Rng = sh.Range("A2:A" & Lst)
    MH = Application.Match(TextBox1.Value, Rng, 0)
    If Not Application.IsNumber(MH) Then GoTo 101:     '如果TextBox1的資料不在A欄中→新增
    R = R + MH
    If Range("B" & R) = TextBox2.Value Then            '否則,比對TextBox2與B欄
'        If Range("C" & MH) <> "" Then GoTo 101:   '??如果C欄非空白格→要不要新增??
        Range("C" & R) = TextBox2.Value            '否則C欄為空白格→C欄=B欄
        Exit Sub                                   '即 司機人員(2)=司機人員(1)
    End If
   
    '重覆上列動作, 直到 R+1>Lst
    Do
        Set Rng = sh.Range("A" & R + 1 & ":A" & Lst)
        MH = Application.Match(TextBox1.Value, Rng, 0)
        If Not Application.IsNumber(MH) Then GoTo 101:
        R = R + MH
        If Range("B" & R) = TextBox2.Value Then
            Range("C" & R) = TextBox2.Value
            Exit Sub
        End If
    Loop Until R + 1 > Lst
101:
    '新增一列資料
    Range("A" & Lst + 1) = TextBox1.Value
    Range("B" & Lst + 1) = TextBox2.Value
End Sub

TOP

回復 5# man65boy
抱歉, 考慮久周, 迼成不便! 幸好有超版的救援!!

TOP

        靜思自在 : 每天無所事事,是人生的消費者,積極、有用才是人生的創造者。
返回列表 上一主題