VBA Excel Problema con ciclo for
Publicado por Johan (2 intervenciones) el 21/11/2019 17:25:18
Estoy ayudando a un amigo a hacer un ahorcado en VBA y tenemos una macro controlando un grupo de CommandButtons del formulario para que cambien el valor dentro de un conjunto de TextBox. El problema es que sólo funciona con la primera letra de cada palabra y no termina de revisar el resto de TextBox con esa misma letra.
Así mismo pongo el código que da las condiciones iniciales en la interfaz donde, por cierto, funciona bien el ciclo for que afecta a las textbox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Public Sub ActBt(bt As CommandButton)
Dim WordBt As String
WordBt = Juego.GWord.Caption
Dim I As Integer
I = 1
Dim Letr As String
Letr = bt.Caption
Dim veri As Boolean
Dim veri2 As Integer
Dim Aux As Integer
veri = False
veri2 = 0
Aux = Juego.Tries.Caption
'///////////////////////////////////////////////////////////////////////////////////////////////
For I = 1 To Len(WordBt)
If Mid(WordBt, I, 1) = Letr Then
Juego.Controls("TextBox" & I).Value = Letr
veri = True
End If
If Juego.Controls("TextBox" & I).Value <> Empty Then veri2 = veri2 + 1
Next I
'///////////////////////////////////////////////////////////////////////////////////////////////////
If veri = False Then Aux = Aux - 1
Juego.Tries.Caption = Aux
If Juego.Tries.Caption = 0 Then
MsgBox "Has sido ahorcado"
Juego.GWord.Visible = True
Game.GameOver
End If
If veri2 = Len(WordBt) Then
MsgBox "Ganaste"
Juego.GWord.Visible = True
End If
'bt.Visible = False
End Sub
Así mismo pongo el código que da las condiciones iniciales en la interfaz donde, por cierto, funciona bien el ciclo for que afecta a las textbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Public Sub playbg()
Dim Aleatorio As Integer
Dim Contraregistros As Integer
Dim LastRow As Integer
Dim ThisWord As String
Dim item As Control
LastRow = 0
'espacio donde esta la palbra a adivinar
Juego.GWord.Caption = Empty
For I = 1 To 23
Juego.Controls("TextBox" & I).Value = Empty
Juego.Controls("TextBox" & I).Visible = False
Next I
For Each item In Juego.Frame1.Controls
item.Visible = True
Next item
contarregistros = Application.WorksheetFunction.CountIf(Sheets(1).Columns(1), registro)
If contarregistros > 0 Then
MsgBox "El registro ya existe"
Else
LastRow = Worksheets("Glosario").Cells(Worksheets("Glosario").Rows.Count, "B").End(xlUp).Row + 1
End If
Aleatorio = Application.WorksheetFunction.RandBetween(1, LastRow) + 1
ThisWord = Worksheets("Glosario").Cells(Aleatorio, 2)
'//////////////////////////////////////////////////////////////////////////////
For I = 1 To Len(ThisWord)
Juego.Controls("TextBox" & I).Visible = True
Next I
'//////////////////////////////////////////////////////////////////////////////
Juego.GWord.Caption = ThisWord
Juego.GWord.Visible = False
Juego.ClueG.Caption = Worksheets("Glosario").Cells(Aleatorio, 4)
Juego.Tries.Caption = Worksheets("Reglas Juego").Cells(1, 1)
End Sub

Valora esta pregunta


0