2017-08-10 59 views
-2

我在使用其他工作簿的一个工作簿中创建宏时遇到问题。创建vba宏的问题

这是我使用的代码:

Private Sub CommandButton21_Click() 
Set wb1 = Workbooks.Open(" 
    http://europort/it_division/ITAC/Portal/Documentations/Proba 1.xlsx" 
         ) 
With wb1 
    FinalRow = .Worksheets("Sheet1") 
       .Cells(.Worksheets("Sheet1").Rows.Count, 1) 
       .End(xlUp).Row 
    For i = 1 To FinalRow 
    If TextBox21.Text = .Worksheets("Sheet1").Cells(i, 1).Value Then 
     MsgBox .Worksheets("Sheet1").Cells(i, 2).Text 
    Next i 
End With 
wb1.Close 
End Sub 

它工作正常,但它打开另一个工作簿,只有当我点击确定按钮,关闭所有工作簿。我不想打开这个工作簿,只是为了看到结果而不想看到整个Excel工作簿。我怎样才能做到这一点?请帮忙。

+0

您是否尝试过在代码开始处设置'Application.ScreenUpdating = False'并在结尾处设置'Application.ScreenUpdating = True'? – YowE3K

+0

你的问题不清楚。当你使用'MsgBox'时,它会暂停应用程序,直到你关闭它。另外'.Worksheets(“Sheet1”).Cells(.Worksheets(“Sheet1”).Rows.Count,1).End(xlUp).Row'必须在一行中,否则你必须用'_'它多线。另外我会建议使用工作表的变量。 – UGP

+0

@UGP该行只有一行,但被一位编辑更改了...... –

回答

0

这应该做你以后,根据你的问题:

Private Sub CommandButton21_Click() 
Application.ScreenUpdating = False 
msg = "" 
Set wb1 = Workbooks.Open("http://europort/it_division/ITAC/Portal/Documentations/Proba 1.xlsx") 
With wb1 
    FinalRow = .Worksheets("Sheet1").Cells(.Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row 
    For i = 1 To FinalRow 
     If TextBox21.Text = .Worksheets("Sheet1").Cells(i, 1).Value Then 
     msg = msg & .Worksheets("Sheet1").Cells(i, 2).Text & vbCr 
    Next i 
End With 
wb1.Close 
Application.ScreenUpdating = True 
If msg <> "" Then MsgBox msg 
End Sub 

整个事情需要整理,你应该申报等,但目前的情况是,如果你的代码工作,这也应该工作。