Visual Basic - Pregunta sobre Instrucción For Each...Next

Life is soft - evento anual de software empresarial
 
Vista:

Pregunta sobre Instrucción For Each...Next

Publicado por SebasSBM (20 intervenciones) el 29/08/2007 15:42:53
He creado una matriz de variables de tipo Variant para uno de mis programas. Después, he creado una instrucción For Each...Next para esta matriz. Cuando pruebo su funcionamiento, me sale una ventana que dice: "Error de compilación. Las variables de la instrucción For Each deben ser de tipo Variant". ¿Cómo es posible que ocurra este error, si la matriz es de tipo Variant? Alguien me lo puede explicar?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Pregunta sobre Instrucción For Each...Next

Publicado por ivan (1039 intervenciones) el 29/08/2007 15:51:55
¿y para qué usar For Each?
¿no te sirve el simple y sencillo For-Next?

pero tal vez no lo estés usando correctamente, checa este simple ejemplo (garantizado, que funciona):

Dim asV(2) As Variant, v As Variant
asV(0) = 1525
asV(1) = "madres"
asV(2) = Date
For Each v In asV
MsgBox v
Next
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Pregunta sobre Instrucción For Each...Next

Publicado por SebasSBM (20 intervenciones) el 29/08/2007 16:08:48
Es complicado. Lo uso para borrar aleatoriamente cuadros de texto escribiendo un cero en lugar del texto original. Es algo así:

Option Explicit
Private Borrar(1 to 36) as Variant ' Los valores de estas variables son números aleatorios del 1 al 81, los cuales no pueden repetirse entre sí.

[...] ' Esto significa que hay más código, pero me lo ahorro aquí porque no viene a cuento.

Public Sub Blanquear()
[...]
For Each Borrar In Borrar
If Borrar = 1 Then A1.Text = 0: A1.Enabled = True
If Borrar = 2 Then A2.Text = 0: A2.Enabled = True
If Borrar = 3 Then A3.Text = 0: A3.Enabled = True
If Borrar = 4 Then A4.Text = 0: A4.Enabled = True
If Borrar = 5 Then A5.Text = 0: A5.Enabled = True
If Borrar = 6 Then A6.Text = 0: A6.Enabled = True
If Borrar = 7 Then A7.Text = 0: A7.Enabled = True
If Borrar = 8 Then A8.Text = 0: A8.Enabled = True
If Borrar = 9 Then A9.Text = 0: A9.Enabled = True
If Borrar = 10 Then B1.Text = 0: B1.Enabled = True
If Borrar = 11 Then B2.Text = 0: B2.Enabled = True
If Borrar = 12 Then B3.Text = 0: B3.Enabled = True
If Borrar = 13 Then B4.Text = 0: B4.Enabled = True
If Borrar = 14 Then B5.Text = 0: B5.Enabled = True
If Borrar = 15 Then B6.Text = 0: B6.Enabled = True
If Borrar = 16 Then B7.Text = 0: B7.Enabled = True
If Borrar = 17 Then B8.Text = 0: B8.Enabled = True
If Borrar = 18 Then B9.Text = 0: B9.Enabled = True
If Borrar = 19 Then C1.Text = 0: C1.Enabled = True
If Borrar = 20 Then C2.Text = 0: C2.Enabled = True
If Borrar = 21 Then C3.Text = 0: C3.Enabled = True
If Borrar = 22 Then C4.Text = 0: C4.Enabled = True
If Borrar = 23 Then C5.Text = 0: C5.Enabled = True
If Borrar = 24 Then C6.Text = 0: C6.Enabled = True
If Borrar = 25 Then C7.Text = 0: C7.Enabled = True
If Borrar = 26 Then C8.Text = 0: C8.Enabled = True
If Borrar = 27 Then C9.Text = 0: C9.Enabled = True
If Borrar = 28 Then D1.Text = 0: D1.Enabled = True
If Borrar = 29 Then D2.Text = 0: D2.Enabled = True
If Borrar = 30 Then D3.Text = 0: D3.Enabled = True
If Borrar = 31 Then D4.Text = 0: D4.Enabled = True
If Borrar = 32 Then D5.Text = 0: D5.Enabled = True
If Borrar = 33 Then D6.Text = 0: D6.Enabled = True
If Borrar = 34 Then D7.Text = 0: D7.Enabled = True
If Borrar = 35 Then D8.Text = 0: D8.Enabled = True
If Borrar = 36 Then D9.Text = 0: D9.Enabled = True
If Borrar = 37 Then E1.Text = 0: E1.Enabled = True
If Borrar = 38 Then E2.Text = 0: E2.Enabled = True
If Borrar = 39 Then E3.Text = 0: E3.Enabled = True
If Borrar = 40 Then E4.Text = 0: E4.Enabled = True
If Borrar = 41 Then E5.Text = 0: E5.Enabled = True
If Borrar = 42 Then E6.Text = 0: E6.Enabled = True
If Borrar = 43 Then E7.Text = 0: E7.Enabled = True
If Borrar = 44 Then E8.Text = 0: E8.Enabled = True
If Borrar = 45 Then E9.Text = 0: E9.Enabled = True
If Borrar = 46 Then F1.Text = 0: F1.Enabled = True
If Borrar = 47 Then F2.Text = 0: F2.Enabled = True
If Borrar = 48 Then F3.Text = 0: F3.Enabled = True
If Borrar = 49 Then F4.Text = 0: F4.Enabled = True
If Borrar = 50 Then F5.Text = 0: F5.Enabled = True
If Borrar = 51 Then F6.Text = 0: F6.Enabled = True
If Borrar = 52 Then F7.Text = 0: F7.Enabled = True
If Borrar = 53 Then F8.Text = 0: F8.Enabled = True
If Borrar = 54 Then F9.Text = 0: F9.Enabled = True
If Borrar = 55 Then G1.Text = 0: G1.Enabled = True
If Borrar = 56 Then G2.Text = 0: G2.Enabled = True
If Borrar = 57 Then G3.Text = 0: G3.Enabled = True
If Borrar = 58 Then G4.Text = 0: G4.Enabled = True
If Borrar = 59 Then G5.Text = 0: G5.Enabled = True
If Borrar = 60 Then G6.Text = 0: G6.Enabled = True
If Borrar = 61 Then G7.Text = 0: G7.Enabled = True
If Borrar = 62 Then G8.Text = 0: G8.Enabled = True
If Borrar = 63 Then G9.Text = 0: G9.Enabled = True
If Borrar = 64 Then H1.Text = 0: H1.Enabled = True
If Borrar = 65 Then H2.Text = 0: H2.Enabled = True
If Borrar = 66 Then H3.Text = 0: H3.Enabled = True
If Borrar = 67 Then H4.Text = 0: H4.Enabled = True
If Borrar = 68 Then H5.Text = 0: H5.Enabled = True
If Borrar = 69 Then H6.Text = 0: H6.Enabled = True
If Borrar = 70 Then H7.Text = 0: H7.Enabled = True
If Borrar = 71 Then H8.Text = 0: H8.Enabled = True
If Borrar = 72 Then H9.Text = 0: H9.Enabled = True
If Borrar = 73 Then I1.Text = 0: I1.Enabled = True
If Borrar = 74 Then I2.Text = 0: I2.Enabled = True
If Borrar = 75 Then I3.Text = 0: I3.Enabled = True
If Borrar = 76 Then I4.Text = 0: I4.Enabled = True
If Borrar = 77 Then I5.Text = 0: I5.Enabled = True
If Borrar = 78 Then I6.Text = 0: I6.Enabled = True
If Borrar = 79 Then I7.Text = 0: I7.Enabled = True
If Borrar = 80 Then I8.Text = 0: I8.Enabled = True
If Borrar = 81 Then I9.Text = 0: I9.Enabled = True
Next[...]
End Sub
[...]

Como puedes ver, lo que pretendo es que según los valores que salgan en las variables, me borren un cuadro de texto concreto, sustituyendo el texto anterior por un cero. No entiendo por qué me dice que tienen que ser variables variant si ya lo son.

Gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Pregunta sobre Instrucción For Each...Next

Publicado por ivan (1039 intervenciones) el 29/08/2007 16:20:16
como te decía, parece que no lo estás usando bien, no puedes llamarle al elemento Borrar si así también se llama tu matriz.
Prueba así:

Public Sub Blanquear()
Dim Elemento as Variant ' Declara esta nueva variable.
[...]
For Each Elemento In Borrar ' Aquí estaba el "meollo"...
' En la variable Elemento (que es Variant) dejará el contenido de cada
' elemento (valga la redundancia) de tu matriz.
If Elemento = 1 Then A1.Text = 0: A1.Enabled = True
etc.

Aunque, creo, que tambien pudiste haber hecho algo así:

Public Sub Blanquear()
Dim Elemento as Integer
[...]
For Elemento = 0 to Ubound( Borrar ) ' 0 o 1, segun Option Base.
If Borrar( Elemento )= 1 Then A1.Text = 0: A1.Enabled = True
etc.

A ver si te ayuda.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar