在excel中从列中填充单词的下拉列表,然后在下拉列表中更改填充相关字段(VBA)的文本框

我有一个单词2007macros的下面的代码,我从一个Excel电子表格中填充客户名称的下拉列表

Private Sub UserForm_Initialize() Dim i As Integer Dim cn As ADODB.Connection Dim rsT As New ADODB.Recordset Set cn = New ADODB.Connection With cn .Provider = "Microsoft.Jet.OLEDB.4.0" .ConnectionString = "Data Source=CCustomers.xls;Extended Properties=Excel 8.0;" .CursorLocation = adUseClient .Open End With rsT.Open "Select distinct * from Customer", cn, adOpenStatic i = 0 With rsT ' This code populates the combo box with the values ' in the YourNamedRange named range in the .xls file. this exampletable is 2 rows by 6 columns and is set as a named range. Do Until .EOF ComboBox_Company.AddItem (i) ComboBox_Company.Column(0, i) = rsT.Fields(0).Value .MoveNext i = i + 1 Loop End With End Sub 

所以我有一个客户名称列,我创build了一个命名范围(客户),它填充下拉列表。 但是,当我在下拉列表中select一个客户时,我想填充两个地址字段(1街道,2个城市)客户的地址。

 Private Sub cbo_customer_Change() Dim customerName As String customerName = cbo_customer.Value End Sub 

电子表格大约有10列,第一个是客户,第九个是地址1,最后一个是地址2。 我如何使用variables客户来填充地址字段? 我必须创build一个新的命名范围与所有的领域,并具有像myRange其中customer = customerNameselect客户,地址1,地址2?

你自己回答了这个问题,用你需要的列创build一个命名范围,然后执行select:

 "SELECT customer, address1, address2 FROM NewRange WHERE customer = '" & customerName & "'" 

到一个新的logging集,并从它的项目0获得地址字段。