これをユーザーフォームのコードとして使用するのはどうですか:
Private Sub UserForm_Initialize()
Dim wks As Worksheet
'loop through all worksheets
For Each wks In ThisWorkbook.Worksheets
'add their names as items to your combo-box
cboTest.AddItem wks.Name
Next wks
End Sub
ただし、これは多くの方法の 1 つにすぎません。ここでは、ワークシート名を配列などの他の形式で保存しておらず、この情報を新しく取得する必要があると想定しています。
また、これはユーザー フォームの初期化のためのルーチンであり、更新ではありません。
Private Sub UserForm_Initialize()
InitCbo
End Sub
Private Sub InitCbo()
Dim wks As Worksheet
With cboTest
'delete all current items of cboTest
.Clear
'loop through all worksheets
For Each wks In ThisWorkbook.Worksheets
'add their names as items to your combo-box
.AddItem wks.Name
Next wks
'select first item
.ListIndex = 0
End With
End Sub
InitCbo
これで、コンボ ボックスを更新するために使用できるようになりました。ボタンをクリックしたとき、または新しいワークシートが追加されたときのように。
List
この Excel ヘルプの例のように、Items も設定できるので、プロパティにも興味があるかもしれません。
Dim MyArray(6,3)
Private Sub UserForm_Initialize()
Dim i As Single
'The 1-st list box contains 3 data columns
ListBox1.ColumnCount = 3
'The 2nd box contains 6 data columns
ListBox2.ColumnCount = 6
'Load integer values into first column of MyArray
For i = 0 To 5
MyArray(i, 0) = i
Next i
'Load columns 2 and three of MyArray
MyArray(0, 1) = "Zero"
MyArray(1, 1) = "One"
MyArray(2, 1) = "Two"
MyArray(3, 1) = "Three"
MyArray(4, 1) = "Four"
MyArray(5, 1) = "Five"
MyArray(0, 2) = "Zero"
MyArray(1, 2) = "Un ou Une"
MyArray(2, 2) = "Deux"
MyArray(3, 2) = "Trois"
MyArray(4, 2) = "Quatre"
MyArray(5, 2) = "Cinq"
'Load data into ListBox1 and ListBox2
ListBox1.List() = MyArray
ListBox2.Column() = MyArray
End Sub
これはListBoxの例ですが、私が知る限り、ComboBoxにも適用されるか、少なくとも同様です。
編集:
貼り付けたコードは常にアクティブなワークブックとアクティブなワークシートを使用するためRange("C2").Value = dayElec
、リスト/コンボボックスの選択したワークシートの値は変更されません。
これを次のように変更する必要があります。
Private Sub CommandButton1_Click()
With ThisWorkbook.Worksheets(cboTest.value)
.Range("D2").Value = nightElec
.Range("F2").Value = dayHeat
.Range("G2").Value = nightHeat
.Range("I2").Value = dayWater
.Range("J2").Value = nightWater
dayElec = Empty
End With
End Sub