具有精确值的用户表单自动填充文本框

问题描述 投票:0回答:1

我想在我的用户表单上自动填充两个文本框,从我的工作表数据中,但我希望代码找到准确的值,而不仅仅是它的一部分。

Ex:如果我输入数字“33”,它会返回一个值,当列中有“202409334”之类的内容时,我只想在整个数字都填满时才假设一个值。

Private Sub txtdevolução_AfterUpdate()



Dim id As String, rowcount As Integer, foundcell As Range


id = txtdevolução.Value
    
    rowcount = Sheets("retornos pendentes").Cells(Rows.Count, 1).End(xlUp).Row
    
    With Worksheets("retornos pendentes").Range("A1:A" & rowcount)
        Set foundcell = .Find(what:=id, LookIn:=xlValues)
        
        If Not foundcell Is Nothing Then
            txtclienteopl.Value = .Cells(foundcell.Row, 3)
            txtmatricula2.Value = .Cells(foundcell.Row, 12)
         Else
            txtclienteopl.Value = ""
            txtmatricula2.Value = ""
            
        End If
        
    End With
    


End Sub
excel vba forms userform vba6
1个回答
0
投票

填写用户表单文本框

  • CDP1802 在评论中检测到当前错误,即您需要将
    LookAt
    参数的参数设置为
    xlWhole
    以获得部分匹配。
  • 另一个问题是
    With Worksheets("retornos pendentes").Range("A1:A" & rowcount)
    ,当你以后使用
    .Cells(foundcell.Row, 3)
    。搜索列为
    A
    列时它可以工作,但如果您更改它,它将返回错误列中的值。使用
    .EntireRow
    实现
    .Columns
    使其更加灵活,并允许使用更加用户友好的列字符串。
Private Sub txtdevoluçao_AfterUpdate()

    Const WS_NAME As String = "retornos pendentes"
    Const FIRST_CELL As String = "A1"
    Const CLIENTEOP_COL As String = "C"
    Const MATRICULA_COL As String = "L"
    
    Dim rg As Range, fCell As Range, lCell As Range
    
    With ThisWorkbook.Sheets(WS_NAME)
        Set fCell = .Range(FIRST_CELL) ' First
        Set lCell = .Cells(.Rows.Count, fCell.Column).End(xlUp) ' Last
        Set rg = .Range(fCell, lCell)
    End With
    
    ' Reusing the variable...
    Set fCell = rg.Find(txtdevoluçao.Value, lCell, xlFormulas, xlWhole) ' Found
    ' ... short for:
    'Set fCell = rg.Find(What:=txtdevoluçao.Value, After:=lCell, _
        LookIn:=xlFormulas, LookAt:=xlWhole) ' Found
   
    If fCell Is Nothing Then
        txtclienteopl.Value = ""
        txtmatricula2.Value = ""
    Else
        With fCell.EntireRow
            txtclienteopl.Value = .Columns(CLIENTEOP_COL).Value
            txtmatricula2.Value = .Columns(MATRICULA_COL).Value
        End With
    End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.