您的位置:首页 > 编程语言 > VB

【VBA研究】进入文本框后其内容全选

2015-07-13 17:57 239 查看
作者:iamlaosong

做一个扫描核对工具,扫描的条码在文本框中,表单中只有一个用于扫描的文本框是有效的,另一个文本框用于显示核对正确时显示货品数量,不过这个文本框用户是不能操作的,如下图所示:



由于需要反复扫描条码,所以希望每次扫描时能覆盖上次扫描的内容,这有两种方法,一种是核对完毕后清除文本框的内容,另一种方法就是将内容全选,显然,第二种方法较好,因为核对完毕后还可以看到上次扫描的内容,如果清空则有点怪怪的,看着不爽,设置全选的代码如下:

。。。

BarCode = TextBox1.Value

。。。

TextBox1.SelStart = 0

TextBox1.SelLength = Len(BarCode)

。。。

附:完整的核对子程序
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
        '取条码值
        BarCode = TextBox1.Value
        '该条码是否本站货品
        For row2 = DatLine To MaxRow2
            If Scan(row2) = BarCode Then
                TextBox2.Value = Qty(row2)
                If Cells(row2, 10) <> "OK" Then
                    OK_No = OK_No + 1
                    Cells(row2, 10) = "OK"
                Else
                    TextBox2.Value = "重复"
                End If
                Exit For
            End If
        Next row2
        '如果没有找到,检查是否为箱码,通过箱码找到货品条码再次核对
        If UserForm1.CheckBox1.Value And row2 > MaxRow2 Then
            '找箱码对应条码
            For k = 1 To MaxRowBox
                If ScanBox(k, 2) = BarCode Then
                    BarCode = ScanBox(k, 1)
                    Exit For
                End If
            Next k
            If k <= MaxRowBox Then
                '找到箱码对应货品条码,再次核对
                For row2 = DatLine To MaxRow2
                    If Scan(row2) = BarCode Then
                        TextBox2.Value = Qty(row2)
                        If Cells(row2, 10) <> "OK" Then
                            OK_No = OK_No + 1
                            Cells(row2, 10) = "OK"
                        Else
                            TextBox2.Value = "重复"
                        End If
                        Exit For
                    End If
                Next row2
            End If
        End If
        
        '货品条码和箱码均不是
        If row2 > MaxRow2 Then
            tmp = MsgBox("本条码非本站货品,条码:" & BarCode, vbOKOnly + vbExclamation, "iamlaosong")
            TextBox2.Value = ""
            ErrNo = ErrNo + 1
        End If
        TextBox1.SelStart = 0
        TextBox1.SelLength = Len(BarCode)
        'TextBox1.Value = ""
    End If
End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: