PDA

View Full Version : Next loop without calling additional functions on error



kuhmuhkuh
08-10-2014, 03:48 PM
Hello,
im quite new to vba and i got a problem in a webscrapper im coding right now.
1) i got this main code which calls all needed functions :


Public Sub maincode()
Dim Playernumber As Integer




Application.ScreenUpdating = False
For Playernumber = 1 To 100
Call ImportWebpageOverall(Playernumber)
Call pulldataOverall
Call ImportWebpageStandardLeagues(Playernumber)
Call pulldataStandardLeague
Call ImportWebpageSpecialEvent(Playernumber)
Call pulldataSpecialEvent
Call ImportWebpageOfflineEvents(Playernumber)
Call pulldataOfflineEvent




Next Playernumber


Call Name(Playernumber)
Call Race(Playernumber)
Call Race2(Playernumber)






Application.ScreenUpdating = True


End Sub

and we got this for the sub pulldataoverall()


Public Sub pulldataOverall()


Dim lastrowsheetone As Integer
Dim findmatch As Variant
Dim findname As String
Dim findelo As Integer
lastrowsheetone = Sheets("Players").UsedRange.Rows.Count
findmatch = Application.Match("1v1 Record:", Sheets("Tabelle3").Range("A1:A200"), 0)

If VarType(findmatch) = vbError Then
Exit Sub

Else

Sheets("Players").Cells(lastrowsheetone + 1, 3) = Sheets("Tabelle3").Cells(findmatch + 1, 2)
Sheets("Players").Cells(lastrowsheetone + 1, 5) = Sheets("Tabelle3").Cells(findmatch + 2, 2)
Sheets("Players").Cells(lastrowsheetone + 1, 7) = Sheets("Tabelle3").Cells(findmatch + 3, 2)
Sheets("Players").Cells(lastrowsheetone + 1, 9) = Sheets("Tabelle3").Cells(findmatch + 4, 2)


Sheets("Players").Cells(lastrowsheetone + 1, 4) = Sheets("Tabelle3").Cells(findmatch + 1, 3)
Sheets("Players").Cells(lastrowsheetone + 1, 6) = Sheets("Tabelle3").Cells(findmatch + 2, 3)
Sheets("Players").Cells(lastrowsheetone + 1, 8) = Sheets("Tabelle3").Cells(findmatch + 3, 3)
Sheets("Players").Cells(lastrowsheetone + 1, 10) = Sheets("Tabelle3").Cells(findmatch + 4, 3)




findname = Application.Match("Record & Games", Sheets("Tabelle3").Range("A1:A200"), 0)


Sheets("Players").Cells(lastrowsheetone + 1, 1) = Sheets("Tabelle3").Cells(findname + 1, 1)
Sheets("Players").Cells(lastrowsheetone + 1, 2) = Sheets("Tabelle3").Cells(findname + 4, 1)
Sheets("Players").Cells(lastrowsheetone + 1, 36) = Sheets("Tabelle3").Cells(findname + 5, 1)


findelo = Application.Match("ELO Rank:", Sheets("Tabelle3").Range("A1:A200"), 0)


Sheets("Players").Cells(lastrowsheetone + 1, 35) = Sheets("Tabelle3").Cells(findelo + 0, 2)


End If
End Sub



written in this sub is : If VarType(findmatch) = vbError Then
Exit sub
What i want now is not to only exit this sub but also dont call the other functions.
Like if this whole code is running for playernumber 1 and an error occurs in the pulldataoverall sub i dont want the code to call the other subs .
I hope its understandable what my problem is.

HaHoBe
08-10-2014, 08:18 PM
Hi, kuhmuhkuh,

instead of

Exit Sub
which just leaves the procedure in which it is called use

End
which will cease the code to stoip at once.

Ciao,
Holger

kuhmuhkuh
08-10-2014, 08:30 PM
so if we have
for playnernumber 1 to 100
and an error occurs on playernumber 7 it will not call additionall subs and just starts the first sub with playernumber 8 ?

HaHoBe
08-11-2014, 10:05 AM
Hi, kuhmuhkuh,

sorry I isunderstood what you are after. My answer was to end all macros and not run on any additionakl number in the loop while you want the loop not to continue with more macros for the playernumber.

You could either convert your Subs to Functions and use a Boolean as return to evaluate and stiore this value in a variable which is checked prior to making another call to another Function. Or you use a Public Variable declared in a standard module, set this Variable to True at the start of the macro.

Maybe have a look at this sample:

Dim lngCtr As Long
Dim blnContinue As Boolean

Sub Main()
For lngCtr = 1 To 3
blnContinue = True
If blnContinue Then Macro1
If blnContinue Then Macro2
Next lngCtr
End Sub

Sub Macro1()
Dim lngWrite As Long

For lngWrite = 1 To 5
Debug.Print "1" & lngCtr & lngWrite
If lngCtr = 2 Then
blnContinue = False
Exit Sub
End If
Next lngWrite
End Sub
Sub Macro2()
Dim lngWrite As Long

For lngWrite = 1 To 5
Debug.Print "2" & lngCtr & lngWrite
Next lngWrite
End Sub
The Immediate Window shows:

111
112
113
114
115
211
212
213
214
215
121
131
132
133
134
135
231
232
233
234
235
HTH,
Hiolger

Paul_Hossler
08-12-2014, 10:38 AM
Not tested but if you make pulldataOverall into a function returning a True/False then you could skip what needs to be skipped



Public Function pulldataOverall() as Boolean


Dim lastrowsheetone As Integer
Dim findmatch As Variant
Dim findname As String
Dim findelo As Integer
lastrowsheetone = Sheets("Players").UsedRange.Rows.Count
findmatch = Application.Match("1v1 Record:", Sheets("Tabelle3").Range("A1:A200"), 0)

pulldataOverall = False
If VarType(findmatch) = vbError Then Exit Function

pulldataOverall = True


etc.







For Playernumber = 1 To 100
Call ImportWebpageOverall(Playernumber)
If pulldataOverall Then
Call ImportWebpageStandardLeagues(Playernumber)
Call pulldataStandardLeague
Call ImportWebpageSpecialEvent(Playernumber)
Call pulldataSpecialEvent
Call ImportWebpageOfflineEvents(Playernumber)
Call pulldataOfflineEvent
Endif



Next Playernumber