在excel文件“A.xlsx
”中 - 我有一个用户表单,其中点击浏览按钮(CommandButton1
),打开另一个excel文件(比如'B.xlsx
)'和名称excelfile中存在的工作表显示在“选择需求计划”组合框中(ComboBox1
)
执行此操作的代码是:
Private Sub CommandButton1_Click()
Dim sFilePath As String
sFilePath = Application.GetOpenFilename()
Workbooks.Open (sFilePath)
For Each sht In ActiveWorkbook.Sheets
Form.ComboBox1.AddItem (sht.Name)
Next sht
End Sub
我希望所选工作簿(Sheet1
)中所选工作表(Say B.xlsx
)的所有列标题(列名称)都显示在其他组合框(Part Number, CSUFI/ATA, Keyword
中)中。 。)
我开始为此编写一个单独的函数:
Private Sub ComboBox1_Change()
cmb = Form.ComboBox1.Value
Dim Cell As Range
For Each Cell In ActiveWorkbook.Worksheets(cmb).Range("A1:AR1")
Form.ComboBox2.AddItem (Cell.Value)
Form.ComboBox3.AddItem (Cell.Value)
Form.ComboBox4.AddItem (Cell.Value)
Form.ComboBox5.AddItem (Cell.Value)
Form.ComboBox6.AddItem (Cell.Value)
Form.ComboBox7.AddItem (Cell.Value)
Form.ComboBox8.AddItem (Cell.Value)
Form.ComboBox9.AddItem (Cell.Value)
Form.ComboBox10.AddItem (Cell.Value)
Form.ComboBox11.AddItem (Cell.Value)
Form.ComboBox12.AddItem (Cell.Value)
Form.ComboBox13.AddItem (Cell.Value)
Next Cell
End Sub
显然,这段代码的问题在于它遍历当前的ActiveWorkbook
,即A.xlsx。我希望它遍历所选的工作簿(B.xlsx)
此外,截至目前,我正在给它一个硬编码范围(A1:AR1),这会导致组合框中的大量空条目。意图是它只循环非空的列标题(有效的列标题)
截至目前的完整代码:
Private Sub ComboBox1_Change()
cmb = Form.ComboBox1.Value
Dim Cell As Range
For Each Cell In ActiveWorkbook.Worksheets(cmb).Range("A1:AR1")
Form.ComboBox2.AddItem (Cell.Value)
Form.ComboBox3.AddItem (Cell.Value)
Form.ComboBox4.AddItem (Cell.Value)
Form.ComboBox5.AddItem (Cell.Value)
Form.ComboBox6.AddItem (Cell.Value)
Form.ComboBox7.AddItem (Cell.Value)
Form.ComboBox8.AddItem (Cell.Value)
Form.ComboBox9.AddItem (Cell.Value)
Form.ComboBox10.AddItem (Cell.Value)
Form.ComboBox11.AddItem (Cell.Value)
Form.ComboBox12.AddItem (Cell.Value)
Form.ComboBox13.AddItem (Cell.Value)
Next Cell
End Sub
Private Sub CommandButton1_Click()
Dim sFilePath As String
sFilePath = Application.GetOpenFilename()
Workbooks.Open (sFilePath)
For Each sht In ActiveWorkbook.Sheets
Form.ComboBox1.AddItem (sht.Name)
Next sht
End Sub
答案 0 :(得分:1)
做这样的事情:
Dim wb As Workbook
Private Sub CommandButton1_Click()
Dim sFilePath As String
sFilePath = Application.GetOpenFilename()
Set wb = Workbooks.Open(sFilePath)
For Each sht In wb.WorkSheets
Form.ComboBox1.AddItem sht.Name
Next sht
End Sub
然后在其他方法中,您可以使用全局wb
代替ActiveWorkbook
第2部分:
Private Sub ComboBox1_Change()
Dim cmb, Cell As Range, rng As Range, sht As Worksheet
cmb = Form.ComboBox1.Value
Set sht = wb.Worksheets(cmb)
'assuming your headers are always on the first row...
Set rng = sht.Range(sht.Range("A1"), _
sht.Cells(1, Columns.Count).End(xlToLeft))
'add some code here to clear the lists first!...
For Each Cell In rng.Cells
If Len(Cell.Value)>0 Then
Form.ComboBox2.AddItem (Cell.Value)
Form.ComboBox3.AddItem (Cell.Value)
Form.ComboBox4.AddItem (Cell.Value)
Form.ComboBox5.AddItem (Cell.Value)
Form.ComboBox6.AddItem (Cell.Value)
Form.ComboBox7.AddItem (Cell.Value)
Form.ComboBox8.AddItem (Cell.Value)
Form.ComboBox9.AddItem (Cell.Value)
Form.ComboBox10.AddItem (Cell.Value)
Form.ComboBox11.AddItem (Cell.Value)
Form.ComboBox12.AddItem (Cell.Value)
Form.ComboBox13.AddItem (Cell.Value)
End If
Next Cell
End Sub