Excel 显示Msgbox如果为false,则退出sub

Excel 显示Msgbox如果为false,则退出sub,excel,vba,Excel,Vba,我有宏,可以让我先选择所需的数据文件,然后继续执行以下步骤。但如果我改变主意,中途取消(在选择文件之前),则会弹出一个消息框,说明“您已取消进程”,并退出子进程 问题是我的宏会立即退出sub,即使我按下了输入数据。我的宏有什么问题导致它们这样做 Sub trial2() Dim wb As Workbook, wb2 As Workbook, wb3 As Workbook Dim ws As Worksheet Dim fn As String Set wb = ActiveWorkbo

我有宏,可以让我先选择所需的数据文件,然后继续执行以下步骤。但如果我改变主意,中途取消(在选择文件之前),则会弹出一个消息框,说明“您已取消进程”,并退出子进程

问题是我的宏会立即退出sub,即使我按下了输入数据。我的宏有什么问题导致它们这样做

Sub trial2()

Dim wb As Workbook, wb2 As Workbook, wb3 As Workbook
Dim ws As Worksheet

Dim fn As String

Set wb = ActiveWorkbook

'this is for the excel to add one more worksheet for the raw data
Set ws = Sheets.Add(After:=Sheets(Worksheets.Count))

Dim ret As Variant
'this whole part is for importing the raw data files into excel
ret = Application.GetOpenFilename("Lkl Files (*.lkl), *.lkl")
If ret <> False Then
Else
MsgBox "You've canceled the process"
With ActiveWorkbook
.Worksheets(.Worksheets.Count).Delete
End With
Exit Sub

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & ret, Destination:=Range("$A$1"))
.Name = ret
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileDecimalSeparator = ","
.TextFileThousandsSeparator = "."
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With
End If


Sheets(2).Activate

'this is to search for the next empty cell and put the date
Dim FirstCell As String
Dim i As Integer
FirstCell = "C19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveCell = datepart(ret)

'this is to filter the raw data into the desired value
ws.Activate
ws.AutoFilterMode = False

'change the value of Criteria1 between "" into the desired value for filtering
ws.Range("$A$9:$P$417").AutoFilter Field:=5, Criteria1:= _
"1"

Range("F31:F401").Select
Selection.Copy



Sheets(2).Activate


'this is for the raw data to be copied into each worksheet

FirstCell = "D19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop


Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Sheets(3).Activate
FirstCell = "C19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveCell = datepart(ret)

ws.Activate

Range("D31:D401").Select
Application.CutCopyMode = False
Selection.Copy


Sheets(3).Activate
FirstCell = "D19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop


Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Sheets(4).Activate
FirstCell = "C19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveCell = datepart(ret)

ws.Activate

Range("G31:G401").Select
Application.CutCopyMode = False
Selection.Copy



Sheets(4).Activate
FirstCell = "D19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop


Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

With ActiveWorkbook
.Worksheets(.Worksheets.Count).Delete
End With
End Sub
Function datepart(filename As Variant) As Date
Dim i As Long
Dim s As String
For i = 1 To Len(filename)
If Mid(filename, i, 8) Like "########" Then
s = Mid(filename, i, 8)
datepart = DateSerial(Right(s, 4), Mid(s, 3, 2), Left(s, 2))
Exit For
End If
Next
End Function
Sub-trial2()
将wb设置为工作簿、wb2设置为工作簿、wb3设置为工作簿
将ws设置为工作表
作为字符串的Dim fn
设置wb=ActiveWorkbook
'这是excel为原始数据再添加一个工作表
设置ws=Sheets.Add(之后:=Sheets(Worksheets.Count))
Dim-ret作为变体
'整个部分用于将原始数据文件导入excel
ret=Application.GetOpenFilename(“Lkl文件(*.Lkl),*.Lkl”)
如果ret为False,则
其他的
MsgBox“您已取消此过程”
使用ActiveWorkbook
.Worksheets(.Worksheets.Count)。删除
以
出口接头
使用ActiveSheet.QueryTables.Add(连接:=_
“TEXT;”&ret,目标:=范围($A$1”))
.Name=ret
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=65001
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
.TextFileDecimalSeparator=“,”
.TextFileThousandsSeparator=“”
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
如果结束
第(2)页。激活
'这是为了搜索下一个空单元格并输入日期
将第一个单元格设置为字符串
作为整数的Dim i
FirstCell=“C19”
范围(第一个单元格)。选择
直到ActiveCell.Value=“”
如果ActiveCell.Value=”“,则
退出Do
其他的
ActiveCell.Offset(1,0)。选择
如果结束
环
ActiveCell=datepart(ret)
'这是为了将原始数据过滤成所需的值
ws.Activate
ws.AutoFilterMode=False
'将“”之间的Criteria1的值更改为所需的筛选值
ws.Range(“$A$9:$P$417”)。自动筛选字段:=5,标准1:=_
"1"
范围(“F31:F401”)。选择
选择,复制
第(2)页。激活
'这用于将原始数据复制到每个工作表中
FirstCell=“D19”
范围(第一个单元格)。选择
直到ActiveCell.Value=“”
如果ActiveCell.Value=”“,则
退出Do
其他的
ActiveCell.Offset(1,0)。选择
如果结束
环
Selection.Paste特殊粘贴:=xlPasteAll,操作:=xlNone,skipblank:=_
False,转置:=True
第(3)页。激活
FirstCell=“C19”
范围(第一个单元格)。选择
直到ActiveCell.Value=“”
如果ActiveCell.Value=”“,则
退出Do
其他的
ActiveCell.Offset(1,0)。选择
如果结束
环
ActiveCell=datepart(ret)
ws.Activate
范围(“D31:D401”)。选择
Application.CutCopyMode=False
选择,复制
第(3)页。激活
FirstCell=“D19”
范围(第一个单元格)。选择
直到ActiveCell.Value=“”
如果ActiveCell.Value=”“,则
退出Do
其他的
ActiveCell.Offset(1,0)。选择
如果结束
环
Selection.Paste特殊粘贴:=xlPasteAll,操作:=xlNone,skipblank:=_
False,转置:=True
第(4)页。激活
FirstCell=“C19”
范围(第一个单元格)。选择
直到ActiveCell.Value=“”
如果ActiveCell.Value=”“,则
退出Do
其他的
ActiveCell.Offset(1,0)。选择
如果结束
环
ActiveCell=datepart(ret)
ws.Activate
范围(“G31:G401”)。选择
Application.CutCopyMode=False
选择,复制
第(4)页。激活
FirstCell=“D19”
范围(第一个单元格)。选择
直到ActiveCell.Value=“”
如果ActiveCell.Value=”“,则
退出Do
其他的
ActiveCell.Offset(1,0)。选择
如果结束
环
Selection.Paste特殊粘贴:=xlPasteAll,操作:=xlNone,skipblank:=_
False,转置:=True
使用ActiveWorkbook
.Worksheets(.Worksheets.Count)。删除
以
端接头
函数datepart(文件名作为变量)作为日期
我想我会坚持多久
像线一样变暗
对于i=1到Len(文件名)
如果Mid(文件名,i,8)喜欢“#######”那么
s=Mid(文件名,i,8)
datepart=DateSerial(右(s,4),中(s,3,2),左(s,2))
退出
如果结束
下一个
端函数
如果在此之后出现以下情况,则缺少结尾:

If ret <> False Then
Else
MsgBox "You've canceled the process"
With ActiveWorkbook
.Worksheets(.Worksheets.Count).Delete
End With
Exit Sub
如果ret为False,则
其他的
MsgBox“您已取消此过程”
使用ActiveWorkbook
.Worksheets(.Worksheets.Count)。删除
以
出口接头
如果在此之后出现以下情况,则缺少结尾:

If ret <> False Then
Else
MsgBox "You've canceled the process"
With ActiveWorkbook
.Worksheets(.Worksheets.Count).Delete
End With
Exit Sub
如果ret为False,则
其他的
MsgBox“您已取消此过程”
使用ActiveWorkbook
.Worksheets(.Worksheets.Count)。删除
以
出口接头
您需要将long With Block后面的“End if”移动到“Exit Sub”后面,如下所示

Dim ret As Variant
'this whole part is for importing the raw data files into excel
ret = Application.GetOpenFilename("Lkl Files (*.lkl), *.lkl")
If ret <> False Then
Else
MsgBox "You've canceled the process"
With ActiveWorkbook
.Worksheets(.Worksheets.Count).Delete
End With
Exit Sub
'**********
'Add this here
'**********
End if

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & ret, Destination:=Range("$A$1"))
.Name = ret
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileDecimalSeparator = ","
.TextFileThousandsSeparator = "."
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With
'**********
'Remove this one
'**********
'End If
Dim ret作为变体
'整个部分用于将原始数据文件导入excel
ret=Application.GetOpenFilename(“Lkl文件(*.Lkl),*.Lkl”)
如果ret为False,则
其他的
MsgBox“您已取消此过程”
使用ActiveWorkbook
.Worksheets(.Worksheets.Count)。删除
以
出口接头
'**********
“把这个加在这里
'**********
如果结束
使用ActiveSheet.QueryTables.Add(连接:=_
“TEXT;”&ret,目标:=范围($A$1”))
.Name=ret
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=65001
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
.TextFileDecimalSeparator=“,”
.TextFileThousandsSeparator=“。