- 帖子
- 522
- 主題
- 36
- 精華
- 1
- 積分
- 603
- 點名
- 0
- 作業系統
- win xp sp3
- 軟體版本
- Office 2003
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2012-12-13
- 最後登錄
- 2021-7-11
|
試試看:
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 |
|