userform vlookup而不使用application.vlookup

时间:2018-03-27 03:41:26

标签: vba excel-vba vlookup userform excel

我目前正在尝试创建一个userform,它将使用列A值调用列B,C,D的匹配值。为了将列B,C和D的值显示到textbox2,textbox3和textbox4中,我必须将列A的值放到textbox1中。我的工作表中的数据是无限期的,因此我不想使用application.vlookup。我已经使用了我的同事提供给我的这个问题的代码,它实际上正在处理我创建的一些用户表单。但是,正如我现在使用它它只是不起作用。

Private Sub Textbox1_AfterUpdate()

If Textbox1.Value = "" Then

Textbox2 = ""
Textbox3 = ""
Textbox4 = ""
Exit Sub

Else

x = 2
Do Until Sheet2.Cells(x, "A") = Textbox1.Value
If Sheet2.Cells(x, "C").Value = "" Then
Textbox2 = ""
Textbox3 = ""
Textbox4 = ""
Exit Sub
End If

x = x + 1
Loop

Textbox2 = Sheet2.Cells(x, "B")
Textbox3 = Sheet2.Cells(x, "C")
Textbox4 = Sheet2.Cells(x, "D")
End If

End Sub

我希望你可以调查一下,指出错误并建议任何更正。

谢谢!

3 个答案:

答案 0 :(得分:2)

您对VLookup的异议无效。无需为查找列创建名称,也无需重做名称范围作为查找值

只需对整个列进行搜索。

那就是说,Application.Match在这种情况下实际上更有用

您的代码,重构

Private Sub Textbox1_AfterUpdate()
    Dim x As Variant
    If Textbox1.Value <> vbNullString Then
        With Sheet2
            x = Application.Match(Textbox1.Value, .Columns(1), 0)
            If Not IsError(x) Then
                If .Cells(x, "C").Value <> vbNullString and x >= 2 Then
                    Textbox2 = .Cells(x, "B")
                    Textbox3 = .Cells(x, "C")
                    Textbox4 = .Cells(x, "D")
                    Exit Sub
                End If
            End If
        End With
    End If
    Textbox2 = vbNullString
    Textbox3 = vbNullString
    Textbox4 = vbNullString
End Sub

在向TextBox1添加数据之前

before adding data to TextBox1

将数据添加到TextBox1后

after adding data to TextBox1

答案 1 :(得分:0)

也许你可以坚持使用vlookup之前的代码,只需稍微改变一下命名范围,并添加另一个命名范围作为左上角数据的标记。

假设你有这张桌子(故意偏离A1):
sample data

命名范围 TopLeftData 设置为 B3 。然后将名称 Names 设置为此公式=OFFSET(TopLeftData,1,0,COUNTA(Sheet1!B:B)-1,2)(您需要更改“Sheet1”或指向该列的另一个名称),然后使用进行vlookup姓名,工作正常。

只要有效数据低于 TopLeftData 且其上方没有数据,名称名称就会动态生效。

答案 2 :(得分:-1)

试试这个:

document.getElementById('upload-file').addEventListener("change", function(e){
    var formData = new FormData();
    var file = e.currentTarget.files[0];
    var reader = new FileReader();

    // The load event is fired each time the reading operation is successfully completed. 
    reader.onload = function(event) {
        var jsonData = JSON.parse(event.target.result);  // event.target.result is the file's data
        reqwest({
            url: '/cgi-bin/upload.cgi',
            type: 'json',
            method: 'post',
            headers: {
                  'enctype': 'multipart/form-data'
                },
            data: { the_file: file },
            error: function (err) {
                alert("There was an error: " + err)
            },
            success: function (data) {

            }
        });  
    }

    reader.readAsText(file);
});
相关问题