我有一本工作簿,可以通过控制按钮导入工作表。目前工作簿正在切换到新导入的工作表,我想防止这种情况发生,因此使用控制按钮保留在工作表上。
这是我的代码:
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
我无法解决它正在切换的代码中的位置,也许我需要输入一些代码才能使其在导入时保持静态?
提前致谢
答案 0 :(得分:1)
将此添加到您的声明块
Dim sThisSht As Worksheet
这也是你设置sThisBk
Set sThisSht = ActiveWorksheet
然后在您还原ScreenUpdating = True
之前,添加此项以返回到开头的活动工作表集:
sThisSht.Activate