VBA:打开新工作簿时宏终止?如何避免?

时间:2018-06-25 12:05:54

标签: excel vba

当提示用户选择Wb时(我从中读取名称并随后打开),我的宏终止或类似的操作。(在此行:“ Application.Workbooks.Open Filename:= sSourceDir”)。我如何保持宏运行?

Sub AppendCognosData()

Dim sSourceDir As String
Dim sFile As String
Dim sSourceFile, sImportSheet2 As String
Dim vfilename, vImportSheet As Variant
Dim SourceWb As Workbook
Dim SourceWs As Worksheet

MsgBox "Select a continuous range of cells (in a column) where numeric values should be appended."

Set Rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8) 'Type Values, 8 - Range object

'Create Ws and Wb for Destionation
Set AppendWs = Worksheets(Rng.Parent.Name)
Set AppendWb = Workbooks(Rng.Parent.Parent.Name)

'Read new range object in input file

sSourceDir = Application.GetOpenFilename( _
FileFilter:="Microsoft Excel Workbooks, *.xls; *.xlsx", Title:="Open Workbook", MultiSelect:=False) 'added MultiSelect:=False
If sSourceDir = "False" Then
    MsgBox "No File Selected!"
    Exit Sub

Else
    vfilename = Split(sSourceDir, "\")
    sFile = vfilename(UBound(vfilename)) 'Get filename incl. format from filepath directory
    Application.Workbooks.Open Filename:=sSourceDir 'Open the Wb

    vSourceFile = Split(sFile, ".")
    sSourceWb = vSourceFile(0) 'Read open Workbook's name

    Set SourceWb = Workbooks(sFile)     

End If
End Sub
  • 打开新的Wb后,我希望用户选择一个范围-即使用“设置Rng2”。

之后,当我在2个不同的Wb中定义了2个范围时,我将进行处理。

0 个答案:

没有答案