我无法从主要来源获取信息

时间:2017-08-01 07:11:00

标签: excel-vba userform vba excel

我创建了一个userform,它将使用ID#自动填充所有信息,但我无法从特定文件夹,工作簿和范围中提取源。

这是我的代码:

Book_stat list

谢谢!

1 个答案:

答案 0 :(得分:1)

请参阅下面的代码中的答案(代码中的注释作为注释):

Option Explicit

Private Sub TextBox4_Change()

Dim wb As Workbook
Dim rSource As Range

' === first set the Workbook object ===
' if the workbook (Excel file) is already open >> use the line below
Set wb = Workbooks("Request ID.xlsm")

' if its close, then use the alternative line below
Set wb = Workbooks.Open("\\Path\")
' now use the Find function    
Set rSource = wb.Worksheets("Sheet1").Range("A:A").Find(What:=TextBox4.Text, LookAt:=xlWhole, MatchCase:=False)
If Not rSource Is Nothing Then '<-- you need to use the same Range variable you used for the Find
     '// Get value in cell r.row, column 2 into textbox2
   TextBox2.Text = Sheet1.Cells(rSource.Row, 4).Value
   ComboBox3.Value = Sheet1.Cells(rSource.Row, 6).Value
   ComboBox4.Value = Sheet1.Cells(rSource.Row, 8).Value
   ComboBox5.Value = Sheet1.Cells(rSource.Row, 9).Value
End If

End Sub