循环遍历特定工作簿(按路径)和特定工作表

时间:2017-09-26 00:17:51

标签: excel vba excel-vba

在excel文件“A.xlsx”中 - 我有一个用户表单,其中点击浏览按钮(CommandButton1),打开另一个excel文件(比如'B.xlsx)'和名称excelfile中存在的工作表显示在“选择需求计划”组合框中(ComboBox1

enter image description here

执行此操作的代码是:

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)

  1. 如何做到这一点?
  2. 此外,截至目前,我正在给它一个硬编码范围(A1:AR1),这会导致组合框中的大量空条目。意图是它只循环非空的列标题(有效的列标题)

    1. 我该怎么做?
    2. 截至目前的完整代码:

      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
      

1 个答案:

答案 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