VBA – USERFORM – 查找值并填充没有activete或select的行

有可能做这样的事情没有select表,或激活? 我需要根据查找键值使用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 

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

 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 

你可以去如下

 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