用户表单输入中的Vlookup

时间:2019-02-11 18:49:15

标签: excel vba

我正在尝试在代码中建立检查,并在文本框中输入用户信息,我正在尝试在表的先前记录中使用vlookup来检查该唯一值是否已被使用(初始化)

目标范围“ erpLots”包含文本格式的单元格,使用VarType()函数检查后我知道,赋值vValue = SpecEntry.TextBox3.Value vValue是字符串类型,我得到的错误是“类型不匹配“是在进行vlookup If Application.VLookup(vValue, erpLots, 1, False) = SpecEntry.TextBox3.Value Then时。

我有种预感,该错误会围绕正在搜索的值“ vValue”与目标范围“ erpLots”之间的类型不匹配引起。

代码如下:

Public intA As Integer
Public foundRow As Double

Sub StartButtonClick()

    Dim rowCount As Long
    Dim ws As Worksheet
    Dim stg As String
    Dim erpLots As Range
    Dim vValue As Variant

    Set erpLots = Worksheets("Inspection Data").Range("C2", Range("C2").End(xlDown))
    Set ws = Worksheets("Inspection Data")

    rowCount = ws.Range("A111111").End(xlUp).Row

    'Checking the userform request info is complete
    If Trim(SpecEntry.TextBox1.Value) = vbNullString Then
        MsgBox "Please enter Operator ID"
    ElseIf Trim(SpecEntry.TextBox2.Value) = vbNullString Then
        MsgBox "Please scan or enter spec. number."
    ElseIf Trim(SpecEntry.TextBox3.Value) = vbNullString Then
        MsgBox "Please scan or enter ERP Lot #."
    Else
        SpecEntry.TextBox1.Value = UCase(SpecEntry.TextBox1.Value)
        SpecEntry.TextBox2.Value = UCase(SpecEntry.TextBox2.Value)
        SpecEntry.TextBox3.Value = UCase(SpecEntry.TextBox3.Value)
        'checking if ERP Lot # already exist in the list

        vValue = SpecEntry.TextBox3.Value
        MsgBox "vValue is: " & vValue

        If Application.VLookup(vValue, erpLots, 1, False) = SpecEntry.TextBox3.Value Then
            foundRow = WorksheetFunction.Match(SpecEntry.TextBox3.Value, erpLots, 1)
            Range("G" & foundRow).Value = Now()
            Range("H" & foundRow).Value = Range("G" & foundRow).Value - Range("E" & foundRow).Value
            Range("H" & foundRow).NumberFormat = "h:mm"
            Range("H" & foundRow).Value = Range("H" & foundRow).Value * 1440
            Range("H" & foundRow).NumberFormat = "000.00"
            intA = 2
            ws.Activate
            With ws.Cells(ws.Rows.Count, Selection.Column).End(xlUp)
                .Select ' not required to change the focus/view
                ActiveWindow.ScrollRow = foundRow - 1
            End With
            Exit Sub
        Else
            With ws.Range("A1")
            intA = 1
            .Offset(rowCount, 0).Value = SpecEntry.TextBox1.Value
            .Offset(rowCount, 1).Value = SpecEntry.TextBox2.Value
            .Offset(rowCount, 2).Value = SpecEntry.TextBox3.Value
            .Offset(rowCount, 3).Value = Now()
            End With
        End If
    End If

End Sub

我的目标是,如果该值存在,则要捕获的信息将记录在同一行但不同的列中,如果该值不存在,则该信息将成为新记录。

2 个答案:

答案 0 :(得分:0)

如果您的Application.match()正常工作,为什么不删除vloopkup而仅:

foundRow = Application.Iferror(WorksheetFunction.Match(SpecEntry.TextBox3.Value, erpLots, 1),0)

那么您的If语句是:

    If foundRow > 0 Then
        Range("G" & foundRow).Value = Now()
        Range("H" & foundRow).Value = Range("G" & foundRow).Value - Range("E" & foundRow).Value
        Range("H" & foundRow).NumberFormat = "h:mm"
        Range("H" & foundRow).Value = Range("H" & foundRow).Value * 1440
        Range("H" & foundRow).NumberFormat = "000.00"
        intA = 2
        ws.Activate
        With ws.Cells(ws.Rows.Count, Selection.Column).End(xlUp)
            .Select ' not required to change the focus/view
            ActiveWindow.ScrollRow = foundRow - 1
        End With
        Exit Sub
    Else
        With ws.Range("A1")
        intA = 1
        .Offset(rowCount, 0).Value = SpecEntry.TextBox1.Value
        .Offset(rowCount, 1).Value = SpecEntry.TextBox2.Value
        .Offset(rowCount, 2).Value = SpecEntry.TextBox3.Value
        .Offset(rowCount, 3).Value = Now()
        End With
    End If

答案 1 :(得分:0)

我使用了countif作为一种检查用户输入是否在目标范围内的方法,然后将其用作if语句中的条件。

Public intA As Integer
Public foundRow As Double

Sub StartButtonClick()

Dim rowCount As Long
Dim ws As Worksheet
Dim stg As String
Dim erpLots As Range
Dim vValue As Variant
Dim count As Integer

Set erpLots = Worksheets("Inspection Data").Range("C2", Range("C2").End(xlDown))
Set ws = Worksheets("Inspection Data")
foundRow = 0
count = 0

rowCount = ws.Range("A111111").End(xlUp).Row

'Checking the userform request info is complete
If Trim(SpecEntry.TextBox1.Value) = vbNullString Then
    MsgBox "Please enter Operator ID"
ElseIf Trim(SpecEntry.TextBox2.Value) = vbNullString Then
    MsgBox "Please scan or enter spec. number."
ElseIf Trim(SpecEntry.TextBox3.Value) = vbNullString Then
    MsgBox "Please scan or enter ERP Lot #."
Else
    SpecEntry.TextBox1.Value = UCase(SpecEntry.TextBox1.Value)
    SpecEntry.TextBox2.Value = UCase(SpecEntry.TextBox2.Value)
    SpecEntry.TextBox3.Value = UCase(SpecEntry.TextBox3.Value)
    'checking if ERP Lot # already exist in the list and is coming back from labs

    vValue = CStr(Trim(SpecEntry.TextBox3.Value))
    count = Application.WorksheetFunction.CountIf(erpLots, vValue)

    If count >= 1 Then
        foundRow = Application.WorksheetFunction.Match(vValue, erpLots, 0) + 1
        MsgBox "row to update is: " & foundRow
        Range("G" & foundRow).Value = Now()
        Range("G" & foundRow).NumberFormat = "mm/dd/yyyy hh:mm"
        Range("H" & foundRow).Value = Range("G" & foundRow).Value - Range("E" & foundRow).Value
        Range("H" & foundRow).NumberFormat = "d " & Chr(34) & "days" & Chr(34) & " , h:mm:ss"
        intA = 2
        ws.Activate
        With ws.Cells(ws.Rows.count, Selection.Column).End(xlUp)
            .Select ' not required to change the focus/view
            ActiveWindow.ScrollRow = foundRow - 1
        End With
        Exit Sub
    Else
        With ws.Range("A1")
        intA = 1
        .Offset(rowCount, 0).NumberFormat = "@"
        .Offset(rowCount, 0).Value = SpecEntry.TextBox1.Value
        .Offset(rowCount, 1).NumberFormat = "@"
        .Offset(rowCount, 1).Value = CStr(SpecEntry.TextBox2.Value)
        .Offset(rowCount, 2).NumberFormat = "@"
        .Offset(rowCount, 2).Value = CStr(SpecEntry.TextBox3.Value)
        .Offset(rowCount, 3).Value = Now()
        End With
    End If
End If

结束子