使用 VBA 打开 Excel 工作簿时如何使打开的工作簿成为活动 window?
When opening Excel workbook using VBA how to make the open workbook the active window?
我正在使用一个 xlsm 文件表单,该表单调用第二个表单来询问用户 select 要打开哪个工作簿。 VBA 代码将打开该工作文件。但是,我发现在 Office 2016 中,文件在原始 xlsm 文件后面打开。也就是不会变成activewindow。我可以使用 Alt+Tab 转到打开的文件,但在我使用 Alt+Tab 激活原始 xlsm 文件之前,该文件不会与我的击键或鼠标单击交互。然后当我反应 VBA-opened 文件时,我可以与之交互。也就是说,VBA 打开的文件将不会变为活动状态,直到我用具有 VBA 代码的工作文件返回基础。我已经尝试了一系列 activeworkbooks,application.activewindow 命令来激活打开的工作簿无济于事。
这是我正在使用的代码:
Private Sub btn_select_Click()
' User clicks on select. If a subcategory is selected open the file and stop program. If not repopulate sub-category
If IsNull(lst_subcategories) Or lst_subcategories = "" Then
lst_subcategories.Clear
Call populate_data
Else ' open file and unload form (End)
If lst_datacategories <> "Tourism" Then
str_filetoopen = "I:\Bureau-Work\Data System\" & lst_datacategories & "\" & lst_subcategories & ".xlsx" ' May need to be F: drive
Else
str_filetoopen = "I:\Bureau-Work\Data System\" & lst_subcategories & "\" & lst_subcategories & ".xlsx" ' May need to be F: drive
End If
On Error Resume Next ' drive or file may not be available. If so error and retain from
Set wb = Workbooks.Open(str_filetoopen)
If wb Is Nothing Then
MsgBox "I: Drive or Retrieve File is Not Accessible"
GoTo handle ' retains frm_editData
End If
Workbooks(wb).Activate
ActiveWindow.Visible = True
End 'exit all macros and forms once successfully loading file
End If
handle:
End Sub
"Workbooks(wb).Activate" 和 "Activewindow.visible=True" 命令试图将新工作簿 window 设置为活动 window。据我所知,这不会发生在 Office 2013 中,但可能是 Office 2016 特有的。
尝试将此添加到 VBA 脚本的末尾:
Application.SendKeys ("%{TAB}")
它将尝试执行 Alt-Tab 切换 windows,希望这会解决您的问题。
试一试:
wb.activate
适合我
我正在使用一个 xlsm 文件表单,该表单调用第二个表单来询问用户 select 要打开哪个工作簿。 VBA 代码将打开该工作文件。但是,我发现在 Office 2016 中,文件在原始 xlsm 文件后面打开。也就是不会变成activewindow。我可以使用 Alt+Tab 转到打开的文件,但在我使用 Alt+Tab 激活原始 xlsm 文件之前,该文件不会与我的击键或鼠标单击交互。然后当我反应 VBA-opened 文件时,我可以与之交互。也就是说,VBA 打开的文件将不会变为活动状态,直到我用具有 VBA 代码的工作文件返回基础。我已经尝试了一系列 activeworkbooks,application.activewindow 命令来激活打开的工作簿无济于事。
这是我正在使用的代码:
Private Sub btn_select_Click()
' User clicks on select. If a subcategory is selected open the file and stop program. If not repopulate sub-category
If IsNull(lst_subcategories) Or lst_subcategories = "" Then
lst_subcategories.Clear
Call populate_data
Else ' open file and unload form (End)
If lst_datacategories <> "Tourism" Then
str_filetoopen = "I:\Bureau-Work\Data System\" & lst_datacategories & "\" & lst_subcategories & ".xlsx" ' May need to be F: drive
Else
str_filetoopen = "I:\Bureau-Work\Data System\" & lst_subcategories & "\" & lst_subcategories & ".xlsx" ' May need to be F: drive
End If
On Error Resume Next ' drive or file may not be available. If so error and retain from
Set wb = Workbooks.Open(str_filetoopen)
If wb Is Nothing Then
MsgBox "I: Drive or Retrieve File is Not Accessible"
GoTo handle ' retains frm_editData
End If
Workbooks(wb).Activate
ActiveWindow.Visible = True
End 'exit all macros and forms once successfully loading file
End If
handle:
End Sub
"Workbooks(wb).Activate" 和 "Activewindow.visible=True" 命令试图将新工作簿 window 设置为活动 window。据我所知,这不会发生在 Office 2013 中,但可能是 Office 2016 特有的。
尝试将此添加到 VBA 脚本的末尾:
Application.SendKeys ("%{TAB}")
它将尝试执行 Alt-Tab 切换 windows,希望这会解决您的问题。
试一试:
wb.activate
适合我