2017-10-13 118 views
0

我有宏讓我選擇我想要的數據文件,然後繼續執行以下步驟。但是如果我想改變主意並取消它(在選擇文件之前),那麼會彈出一個消息框,指出「您已取消流程」並退出該子文件夾。顯示Msgbox if false然後退出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 
+0

剛剛測試過你的代碼,它彈出一個msgbox然後退出子。您可以設置一箇中斷點來查看正在發生的事情。 – newacc2240

+0

我看到exit sub後有很多代碼。那永遠不會執行。對? – Valli

+0

是的,如果我自己取消流程,它會聲明我已取消流程。但是,如果我真的要按文件輸入,它仍然會顯示我取消了進程並退出子。你找到我了嗎?哈哈 – Fong

回答

1

您需要將「結束,如果」這是經過你的長帶座是「退出小組」之後像這樣

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 
+0

試過把它移動起來,但仍然沒有做我想做的事......如果沒有問題,只是當我選擇一個輸入文件時,它仍然說我取消了過程並退出子。這意味着,退出子之後的所有內容,將不會被處理,無論我是否選擇並輸入或不是。 – Fong

+0

您是否嘗試在「If ret <> False Then」行上放置斷點?然後,您可以運行宏並將鼠標懸停在「ret」上以查看傳遞給變量的值。 – DecimalTurn

+0

我不確定你的意思。我問的是,當線條以黃色突出顯示時,ret的值是多少。 – DecimalTurn

1

缺少的結束如果在此之後:

If ret <> False Then 
Else 
MsgBox "You've canceled the process" 
With ActiveWorkbook 
.Worksheets(.Worksheets.Count).Delete 
End With 
Exit Sub 
+0

有一個結束如果之後與塊。 – newacc2240

+0

@ newacc2240不夠好..... ***用ActiveSheet.QueryTables ***不能執行。 –