VBA - cómo saltar condicionalmente una iteración de bucle for
Tengo un bucle for sobre una matriz. Lo que quiero hacer es probar una determinada condición en el bucle y saltar a la siguiente iteración si es true:
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
If (Schedule(i, 1) < ReferenceDate) Then
PrevCouponIndex = i
Continue '*** THIS LINE DOESN'T COMPILE, nor does "Next"
End If
DF = Application.Run("SomeFunction"....)
PV = PV + (DF * Coupon / CouponFrequency)
Next
Sé que puedo hacer:
If (Schedule(i, 1) < ReferenceDate) Then Continue For
Pero quiero poder registrar el último valor de i en la variable PrevCouponIndex.
¿Alguna idea?
Gracias
6 answers
¿No podrías simplemente hacer algo simple como esto?
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
If (Schedule(i, 1) < ReferenceDate) Then
PrevCouponIndex = i
Else
DF = Application.Run("SomeFunction"....)
PV = PV + (DF * Coupon / CouponFrequency)
End If
Next
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-30 15:02:50
VBA no tiene un Continue
o cualquier otra palabra clave equivalente para saltar inmediatamente a la siguiente iteración del bucle. Yo sugeriría un uso juicioso de Goto
como una solución alternativa, especialmente si esto es solo un ejemplo artificial y su código real es más complicado:
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
If (Schedule(i, 1) < ReferenceDate) Then
PrevCouponIndex = i
Goto NextIteration
End If
DF = Application.Run("SomeFunction"....)
PV = PV + (DF * Coupon / CouponFrequency)
'....'
'a whole bunch of other code you are not showing us'
'....'
NextIteration:
Next
Si eso es realmente todo tu código, sin embargo, @Brian es absolutamente correcto. Simplemente ponga una cláusula Else
en su declaración If
y termine con ella.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-30 23:08:01
Continue For
no es válido en VBA o VB6.
Desde esta página de MSDN parece haber sido introducida en VB.Net en VS 2005.Neto 2.
Como los otros han dicho, no hay realmente una opción que no sea usar Goto
o un Else
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-30 15:08:56
Puedes usar un tipo de continue
usando un Do ... Loop While False
anidado:
'This sample will output 1 and 3 only
Dim i As Integer
For i = 1 To 3: Do
If i = 2 Then Exit Do 'Exit Do is the Continue
Debug.Print i
Loop While False: Next i
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-26 08:21:05
Hola, también me enfrento a este problema y lo resuelvo usando el siguiente código de ejemplo
For j = 1 To MyTemplte.Sheets.Count
If MyTemplte.Sheets(j).Visible = 0 Then
GoTo DoNothing
End If
'process for this for loop
DoNothing:
Next j
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-05 04:48:41
Tal vez intente poner todo al final si y use un else para omitir el código, esto lo hará de modo que no pueda usar el GoTo.
If 6 - ((Int_height(Int_Column - 1) - 1) + Int_direction(e, 1)) = 7 Or (Int_Column - 1) + Int_direction(e, 0) = -1 Or (Int_Column - 1) + Int_direction(e, 0) = 7 Then
Else
If Grid((Int_Column - 1) + Int_direction(e, 0), 6 - ((Int_height(Int_Column - 1) - 1) + Int_direction(e, 1))) = "_" Then
Console.ReadLine()
End If
End If
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-30 20:56:35