在Userform中使用组合框打开工作表

时间:2015-01-16 16:00:47

标签: vba vlookup

我正在尝试创建用户表单。在第一个框中,它将列出所有打开的工作簿,第二个框将列出所选工作簿中的所有打开的工作表。所以最后我希望userform基本上创建一个vlookup公式。  以下是我到目前为止的情况:

Option Explicit  Private Sub UserForm_Initialize() 

     Dim wkb    As Workbook 

     With Me.ComboBox1 
         For Each wkb In Application.Workbooks 
             .AddItem wkb.Name 
         Next wkb 
         .ListIndex = 0 
     End With  
End Sub   

Private Sub ComboBox1_Change() 

     Dim wks As Worksheet, wkb As Workbook 
     Me.CommandButton1.Tag = Workbooks(Me.ComboBox1.Value).Path 
     For Each wkb In Application.Workbooks 
         If wkb.Name Like Me.ComboBox1.Value Then 
             Me.ComboBox2.Clear 
             For Each wks In Workbooks(wkb.Name).Worksheets 
                 Me.ComboBox2.AddItem wks.Name 
             Next wks 
             Me.ComboBox2.ListIndex = 0  Exit Sub  End If  Next wkb  
End Sub    

Private Sub CommandButton1_Click() 

 ThisWorkbook.Worksheets("Sheet1").Range("G3").Formula =
 "=VLOOKUP(C3,[" & Me.ComboBox1.Value & "]" & Me.ComboBox2.Value &"!B3:C5,2,FALSE)"  

Workbooks(Me.ComboBox1.Value).Close  
End Sub

我遇到问题的代码部分是"=VLOOKUP(C3,[" & Me.ComboBox1.Value & "]" & Me.ComboBox2.Value &"!B3:C5,2,FALSE)",因为每当我尝试引用另一个工作簿时,它都会给我一个错误。

有人能解释一下这个问题吗?

0 个答案:

没有答案