防止工作簿切换到新导入的工作表

我有一个工作簿,通过​​控制button导入工作表。 目前该工作簿正在切换到新导入的工作表,我想阻止这种情况的发生,所以用控制button留在工作表上。

这是我的代码:

Sub ImportSheet() Dim sImportFile As String, sFile As String Dim sThisBk As Workbook Dim vfilename As Variant Application.ScreenUpdating = False Application.DisplayAlerts = False Set sThisBk = ActiveWorkbook sImportFile = Application.GetOpenFilename( _ FileFilter:="Microsoft Excel Workbooks, *.xls; *.xlsx", Title:="Open Workbook") If sImportFile = "False" Then MsgBox "No File Selected!" Exit Sub Else vfilename = Split(sImportFile, "\") sFile = vfilename(UBound(vfilename)) Application.Workbooks.Open Filename:=sImportFile Set wbBk = Workbooks(sFile) With wbBk If SheetExists(sWSName) Then Set wsSht = .Sheets(sWSName) wsSht.Copy after:=sThisBk.Sheets("Sheet3") Else MsgBox "There is no sheet with name :Raw_Data in:" & vbCr & .Name End If wbBk.Close SaveChanges:=False End With End If Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub Private Function SheetExists(sWSName) As Boolean Dim ws As Worksheet On Error Resume Next sWSName = InputBox("Enter sheet name") Set ws = Worksheets(sWSName) If Not ws Is Nothing Then SheetExists = True End Function 

我无法弄清楚它正在切换的代码中的位置,也许我需要放置一些代码以使其在导入时保持静态?

提前致谢

将此添加到您的声明块

 Dim sThisSht As Worksheet 

而这个地方你也设置了sThisBk

 Set sThisSht = ActiveWorksheet 

然后,在恢复ScreenUpdating = True ,将其添加到开始时返回到活动图纸集:

 sThisSht.Activate