VBA - USERFORM - 查找值并填充行而不使用activete或select

时间:2017-02-06 07:18:02

标签: excel vba excel-vba

可以在没有选择表的情况下执行此类操作,还是激活?我需要根据查找键值在userform中更改值。

Dim sonsat As Long
Sheets("DATA").Range("A:A").Find(Keycombobox.Text)
sonsat = ActiveCell.Row
Cells(sonsat, 1) = TextBox1
Cells(sonsat, 2) = TextBox2
Cells(sonsat, 3) = TextBox3
Cells(sonsat, 4) = TextBox4
Cells(sonsat, 5) = TextBox5
Cells(sonsat, 6) = TextBox6
Cells(sonsat, 7) = TextBox7

2 个答案:

答案 0 :(得分:1)

使用下面的Find方法(使用SelectActiveCellActivate进行W / O)。

Option Explicit

Sub TestFind()

Dim sonsat As Long
Dim FindRng As Range

With Sheets("DATA")
    Set FindRng = .Range("A:A").Find(Keycombobox.Text) ' <-- assuming  Keycombobox is a textBox

    If Not FindRng Is Nothing Then ' <-- successful find
        sonsat = FindRng.Row

        ' rest of yout code here ....
        .Cells(sonsat, 1) = TextBox1 '<-- for good coding practice use TextBox1.Value ' or TextBox1.Text
        .Cells(sonsat, 2) = TextBox2
        .Cells(sonsat, 3) = TextBox3
        .Cells(sonsat, 4) = TextBox4
        .Cells(sonsat, 5) = TextBox5
        .Cells(sonsat, 6) = TextBox6
        .Cells(sonsat, 7) = TextBox7
    Else
        MsgBox "Unable to find " & Keycombobox.Text & " in specified Range !"
    End If
End With

End Sub

答案 1 :(得分:0)

你可以像下面那样

Dim i As Long

With Sheets("DATA").Range("A:A").Find(Keycombobox.Text)
    For i = 1 To 7
        Cells(.Row, i) = Me.Controls("TextBox" & i)
    Next
End With