|
-
Jun 8th, 2010, 10:50 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Procedure Stops Unexpectedly
I am using VBA in Excel 2007.
I tried runningthrough the code step-by-step and once you try to step past the "Selection.Clear" statement the program ends as if someone clicked on the Stop/Reset Button. I have no clue as to why this is happening. I don't even receive an error so I can no clue as to where I should start looking for the problem.
Code:
Sub Import_Data(strDate As Date)
'This procedure imports the data for the month requested in the variable strDate.
Sheets("RawData").Select
Range("A2:AK50").Select
Selection.Clear
'The error handler informs the user with the developer's name should there be a problem
'importing the data.
On Error GoTo ErrorHandler
Sheets("RawData").Activate
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_1_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$C$1"))
.Name = "Test_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_2_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$F$1"))
.Name = "Test_2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_3_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$I$1"))
.Name = "Test_3"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_4_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$V$1"))
.Name = "Test_4"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_5_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$Z$1"))
.Name = "Test_5"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Range("AK2").Value = DateAdd("m", -1, strDate)
'Calls the procedure containing the date the data was last updated
Call Last_Updated
Call Report1
Call Report2
Call Report3
Call Report4
Call Report5
Exit Sub
ErrorHandler:
Call MsgBox(Format(DateAdd("m", -1, strDate), "mmmm") & "'s data is not available at this time. Please try again after you receive the email that the data has been updated. If you received this message after receiving an email that the data has already been updated for " & Format(DateAdd("m", -1, strDate), "mmmm") & ", please contact Developer Name (ID123).", vbOKOnly, "Data Not Found")
End Sub
-
Jun 8th, 2010, 11:07 AM
#2
Re: Procedure Stops Unexpectedly
Call Last_Updated
Call Report1
Call Report2
Call Report3
Call Report4
Call Report5
If I remove this from your code and run it then I see the message box so definitely the error lies somewhere in the above subs...
Can I see that as well?
BTW, this is what I tried...
Code:
Sub sample()
Import_Data #12/12/2009#
End Sub
Sub Import_Data(strDate As Date)
Dim ws As Worksheet
Set ws = Sheets("RawData")
ws.Range("A2:AK50").Clear
On Error GoTo ErrorHandler
With ws.QueryTables.Add(Connection:="URL;http://etc/etc/Test_1_" & _
Format(strDate, "mmyy") & ".txt", Destination:=Range("$C$1"))
.Name = "Test_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ws.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_2_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$F$1"))
.Name = "Test_2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ws.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_3_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$I$1"))
.Name = "Test_3"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ws.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_4_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$V$1"))
.Name = "Test_4"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ws.QueryTables.Add(Connection:= _
"URL;http://etc/etc/Test_5_" & Format(strDate, "mmyy") & ".txt", _
Destination:=Range("$Z$1"))
.Name = "Test_5"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Range("AK2").Value = DateAdd("m", -1, strDate)
Exit Sub
ErrorHandler:
Call MsgBox(Format(DateAdd("m", -1, strDate), "mmmm") & _
"'s data is not available at this time. " & _
"Please try again after you receive the email that the data has been updated. " & _
"If you received this message after receiving an email that the data has already" & _
"been updated for " & Format(DateAdd("m", -1, strDate), "mmmm") & _
", please contact Developer Name (ID123).", vbOKOnly, "Data Not Found")
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jun 8th, 2010, 11:37 AM
#3
Thread Starter
Hyperactive Member
Re: Procedure Stops Unexpectedly
Thanks for the help Kool. I tried removing those Call's, but I still had the same issue. However, I did rewrite the top part as you did "Dim ws as Worksheet...etc...etc" and it runs just fine now.
I've always written it the way I had it previously and I've never had a problem. I don't know why it's being so anal now. Though, it probably is better practice to declare it as you did.
Thanks again.
-
Jun 8th, 2010, 11:54 AM
#4
Re: [RESOLVED] Procedure Stops Unexpectedly
Glad it is resolved
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|