小弟我有亂拼湊掃描程式,目前卡在TextBox1 & TextBox2無法和B & C比對資料,想要TextBox1 & TextBox2掃出數值,會和B & C列比對,有重複的發出警告並自動返回TextBox1,謝謝大神
比對------------------
Dim WshShell, EE
ActiveSheet.DisplayPageBreaks = False
On Error Resume Next
s = TextBox1
For i = 2 To UsedRange.Rows.Count
If s = Cells(i, 2) Then GoTo EE
If KeyCode = 13 Then
Exit For
End If
Next
EE: MsgBox WshShell.popup
Set WshShell = CreateObject("WScript.Shell")
EE = WshShell.popup(BtnCode, 1, "MsgBox " & s & "和B列第" & i & "行值重複。", 16)
ActiveSheet.DisplayPageBreaks = True
掃描------------------
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim WshShell, EE
On Error Resume Next
If KeyCode = 13 Then
bcr = True
If Me.Controls("TextBox1") <> "" Then
If Me.Controls("TextBox1") = Me.Controls("TextBox2") Then GoTo EE
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 2) = TextBox1.Text
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 3) = TextBox2.Text
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 1) = Date
ElseIf bcr = True Then
bcr = False
If KeyCode = 13 Then CommandButton1_Click
Else
End If
For Each c In UserForm1.Controls
If TypeName(c) = "TextBox" Then c.Text = ""
If KeyCode = 13 Then CommandButton1_Click
Next
Exit Sub
EE: MsgBox WshShell.popup
Set WshShell = CreateObject("WScript.Shell")
EE = WshShell.popup(BtnCode, 1, "重複", 16)
Me.TextBox1.SetFocus
For Each c In UserForm1.Controls
If TypeName(c) = "TextBox" Then c.Text = ""
If KeyCode = 13 Then CommandButton1_Click
Next
End If
End Sub
Private Sub CommandButton1_Click()
r = Len(Me.TextBox1.Text)
Me.TextBox1.SetFocus
Me.TextBox1.SelStart = 0
Me.TextBox1.SelLength = r
KeyCode = 13
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
不知道你的B跟C在哪,
TextBox取值裡面就有了,Me.Controls("TextBox1")
這個就是了,
拿來比較就好了.
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim WshShell, EE
Dim intLen As Integer
On Error Resume Next
If KeyCode = 13 Then
If Len(Me.TextBox1.Text) And Len(Me.TextBox1.Text) <> 16 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup "※字數不正確,請重新輸入!", 1, ""
Else
If Me.Controls("TextBox1") <> "" Then
If Me.Controls("TextBox1") = Me.Controls("TextBox2") Then GoTo EE
bcr = True
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 2) = TextBox1.Text
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 3) = TextBox2.Text
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 1) = Date
ElseIf bcr = True Then
bcr = False
Else
End If
End If
End If
If KeyCode = 13 Then CommandButton1_Click
Exit Sub
EE: MsgBox WshShell.popup
Set WshShell = CreateObject("WScript.Shell")
EE = WshShell.popup(BtnCode, 1, "重複", 16)
If KeyCode = 13 Then CommandButton1_Click
End Sub
Private Sub CommandButton1_Click()
r = Len(Me.TextBox1.Text)
Me.TextBox1.SetFocus
Me.TextBox1.SelStart = 0
Me.TextBox1.SelLength = r
KeyCode = 13
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r&, i&
If Target.Column = 2 And Target.Count = 1 Then
r = Target.Row
i = Application.CountIf(Range("B1:B" & r), Target.Value)
If i > 1 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup Target.Value & "重複,請重新輸入!", 1, ""
End If
End If
If Target.Column = 3 And Target.Count = 1 Then
r = Target.Row
i = Application.CountIf(Range("C1:C" & r), Target.Value)
If i > 1 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup Target.Value & "重複,請重新輸入!", 1, ""
End If
End If
End Sub
已經拼湊完成了,但現在出現另個問題,就是掃描,記錄到欄位後,會出現重複的問題,應該是超過16碼的問題吧,請問有解嗎?謝謝
方便提供檔案嗎?
檔案:https://www.sendspace.com/file/j0ss1w
恩恩,
我晚上回去再幫你看看.
感恩
所以你的需求是甚麼?
我沒看到你在哪裡有做比對的動作...
另外...你的排版好亂...
哈哈,因為我都是參考來的,拼拼湊湊的結果.....
原本的想法是在TextBox做比對,TextBox1 & TextBox2 重複、不記錄加警示,TextBox1 & TextBox2 和資料庫重複、不記錄加警示。
改到最後,TextBox1 & TextBox2 和資料庫重複,會記錄,然後需手動刪,以及超過16碼,尾2碼未進位前,會有重複警示
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim WshShell, EE
Dim intLen As Integer
On Error Resume Next
If KeyCode = 13 Then
If Len(Me.TextBox1.Text) And Len(Me.TextBox1.Text) <> 16 Then '固定16碼
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup "客戶字數不正確,請重新輸入!", 1, "", 64
Else
If Len(Me.TextBox1.Text) And Len(Me.TextBox2.Text) < 18 Then '18碼~24碼
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup "自家字數不正確,請重新輸入!", 1, "", 64
Else
If Me.Controls("TextBox1") <> "" Then
If Me.Controls("TextBox1") = Me.Controls("TextBox2") Then GoTo EE
bcr = True
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 2) = "S/N:" + TextBox1.Text '直接加"S/N:" (目前先這樣解決)
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 3) = TextBox2.Text
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 1) = Date 'A欄加上今天日期
ElseIf bcr = True Then
bcr = False
Else
End If
End If
End If
End If
If KeyCode = 13 Then CommandButton1_Click
Exit Sub
EE: MsgBox WshShell.popup
Set WshShell = CreateObject("WScript.Shell")
EE = WshShell.popup(BtnCode, 1, "重複", 16)
If KeyCode = 13 Then CommandButton1_Click
End Sub
Private Sub CommandButton1_Click()
r = Len(Me.TextBox1.Text)
Me.TextBox1.SetFocus
Me.TextBox1.SelStart = 0
Me.TextBox1.SelLength = r
KeyCode = 13
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r&, i&
If Target.Column = 2 And Target.Count = 1 Then
r = Target.Row
i = Application.CountIf(Range("B1:B" & r), Target.Value)
If i > 1 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup Target.Value & "重複,請重新輸入!", 1, "", 64
End If
End If
If Target.Column = 3 And Target.Count = 1 Then
r = Target.Row
i = Application.CountIf(Range("C1:C" & r), Target.Value)
If i > 1 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.popup Target.Value & "重複,請重新輸入!", 1, "", 64
End If
End If
Range("A" & Rows.Count).End(xlUp).Select
End Sub
不過現在還沒加入資料庫的部分吧,
那可能要查一下相關資料..