我是vb的新手,我正在尝试在Excel 2010中编写简单的程序。 我有客户的多页(cutomer1,customer2,...,customer20) 对于每个客户,我有10个用户“标签”及其文本框。 文本框由页面customer1 B9:B18,customer2 D9:D18等填写...
有没有办法动态获取名称和值以避免代码如下所示?
CUSTOMER1_USER1_TEXTBOX.Value = Sheets("Data").Range("B9").Value
CUSTOMER1_USER2_TEXTBOX.Value = Sheets("Data").Range("B10").Value
CUSTOMER1_USER3_TEXTBOX.Value = Sheets("Data").Range("B11").Value
CUSTOMER1_USER4_TEXTBOX.Value = Sheets("Data").Range("B12").Value
CUSTOMER1_USER5_TEXTBOX.Value = Sheets("Data").Range("B13").Value
CUSTOMER1_USER6_TEXTBOX.Value = Sheets("Data").Range("B14").Value
CUSTOMER1_USER7_TEXTBOX.Value = Sheets("Data").Range("B15").Value
CUSTOMER1_USER8_TEXTBOX.Value = Sheets("Data").Range("B16").Value
CUSTOMER1_USER9_TEXTBOX.Value = Sheets("Data").Range("B17").Value
CUSTOMER1_USER10_TEXTBOX.Value = Sheets("Data").Range("B18").Value
CUSTOMER2_USER1_TEXTBOX.Value = Sheets("Data").Range("D9").Value
CUSTOMER2_USER2_TEXTBOX.Value = Sheets("Data").Range("D10").Value
CUSTOMER2_USER3_TEXTBOX.Value = Sheets("Data").Range("D11").Value
CUSTOMER2_USER4_TEXTBOX.Value = Sheets("Data").Range("D12").Value
CUSTOMER2_USER5_TEXTBOX.Value = Sheets("Data").Range("D13").Value
CUSTOMER2_USER6_TEXTBOX.Value = Sheets("Data").Range("D14").Value
CUSTOMER2_USER7_TEXTBOX.Value = Sheets("Data").Range("D15").Value
CUSTOMER2_USER8_TEXTBOX.Value = Sheets("Data").Range("D16").Value
CUSTOMER2_USER9_TEXTBOX.Value = Sheets("Data").Range("D17").Value
CUSTOMER2_USER10_TEXTBOX.Value = Sheets("Data").Range("D18").Value
...
你能帮帮我吗?
由于
答案 0 :(得分:2)
这样的事情应该有效:
Private Sub UserForm_Initialize()
Dim i As Long, j As Long
With Sheets("Data")
For i = 1 To 20
For j = 1 To 10
Me.Controls("CUSTOMER" & i & "_USER" & j & "_TEXTBOX").Value = .Cells(j+8, 2*i).Value
Next j
Next i
End With
End Sub