Notifications
Clear all

Caption não funciona em um dos OptionButton

3 Posts
2 Usuários
0 Reactions
1,163 Visualizações
(@lzm11)
Posts: 0
New Member
Topic starter
 

Boa tarde,

Estou desenvolvendo um códio, porém apenas os OptionButtons 4, 5 e 6 não estão funcionando. Todo o restante é captado e jogado na planilha, exceto estes 3.

Com o código abaixo, é possível descobrir o motivo?

Muito obrigado desde já!

Private Sub CommandButton1_Click()

Dim UltimaLinhas As Long
UltimaLinha = Sheets("BookLog").Cells(Cells.Rows.Count, 2).End(xlUp).Row
If UltimaLinha < 7 Then
UltimaLinha = 7
Else
UltimaLinha = UltimaLinha + 1
End If


Range("B" & UltimaLinha).Value = TextBox2.Text


If OptionButton1.Value = True Then
    Range("C" & UltimaLinha).Value = OptionButton1.Caption
ElseIf OptionButton2.Value = True Then
    Range("C" & UltimaLinha).Value = OptionButton2.Caption
ElseIf OptionButton3.Value = True Then
    Range("C" & UltimaLinha).Value = OptionButton3.Caption
End If


If OptionButton4.Value = True Then
    Range("D" & UltimaLinha).Value = OptionButton4.Caption
ElseIf OptionButton5.Value = True Then
    Range("D" & UltimaLinha).Value = OptionButton5.Caption
ElseIf OptionButton6.Value = True Then
    Range("D" & UltimaLinha).Value = OptionButton6.Caption
End If


If OptionButton7.Value = True Then
    Range("E" & UltimaLinha).Value = OptionButton7.Caption
ElseIf OptionButton8.Value = True Then
    Range("E" & UltimaLinha).Value = OptionButton8.Caption
ElseIf OptionButton9.Value = True Then
    Range("E" & UltimaLinha).Value = OptionButton9.Caption
End If


If OptionButton10.Value = True Then
    Range("F" & UltimaLinha).Value = OptionButton10.Caption
ElseIf OptionButton11.Value = True Then
    Range("F" & UltimaLinha).Value = OptionButton11.Caption
ElseIf OptionButton12.Value = True Then
    Range("F" & UltimaLinha).Value = OptionButton12.Caption
End If


Range("G" & UltimaLinha).Value = TextBox3.Text

Range("H" & UltimaLinha).Value = TextBox4.Text

lsLimparTextBox UserForm1
    
    MsgBox "Cadastro Realizado com Sucesso!", vbDefaultButton1, "CADASTRO DE DADOS"
    TextBox2.SetFocus


End Sub





Public Function lsInserirTextBox(formulario As UserForm, ByVal lSheet As String, ByVal lColunaCodigo As Long)
    Dim controle            As Control
    Dim lUltimaLinhaAtiva   As Long
    
    lUltimaLinhaAtiva = Worksheets(lSheet).Cells(Worksheets(lSheet).Rows.Count, lColunaCodigo).End(xlUp).Row + 1
    
    For Each controle In formulario.Controls
        lsInserir controle, lSheet, lColunaCodigo, lUltimaLinhaAtiva
    Next
End Function


Public Function lsLimparTextBox(formulario As UserForm)
    Dim controle            As Control
    
    For Each controle In formulario.Controls
        If TypeOf controle Is MSForms.TextBox Then
            controle.Text = ""
        End If
    Next
End Function


Private Sub CommandButton2_Click()
    lsLimparTextBox frmCadastro
    
    TextBox1.SetFocus
End Sub

Private Sub TextBox2_Change()
If Len(TextBox2) = 2 Or Len(TextBox2) = 5 Then
        TextBox2.Text = TextBox2.Text & "/"
        SendKeys "{End}", True
    End If
End Sub


Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

   TextBox2.MaxLength = 10
 
    If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
        KeyAscii = 0
    End If
End Sub

 
Postado : 07/10/2019 1:59 pm
(@srobles)
Posts: 0
New Member
 

Izm11,

Como não informou se está utilizando os controles através de Formulários(UserForms) ou se é através de controles na própria guia, vou arriscar a dar um palpite esperando que seja em Userforms.

Experimente o que segue abaixo :

Private Sub CommandButton1_Click()

Dim UltimaLinhas As Long
Dim ctlOpt As Control
Dim contador As Long

UltimaLinha = ThisWorkbook.Sheets("BookLog").Cells(Cells.Rows.Count, 2).End(xlUp).Row
If UltimaLinha < 7 Then
UltimaLinha = 7
Else
UltimaLinha = UltimaLinha + 1
End If


Range("B" & UltimaLinha).Value = TextBox2.Text
    
    contador = 0
    
    For Each ctlOpt In Me.Controls
        If TypeName(ctlOpt) = "OptionButton" Then
            If ctlOpt = True Then
                contador = contador + 1
                
                Select Case contador
                    Case Is = 1
                        Range("C" & UltimaLinha).Value = Me.Controls(ctlOpt.Caption).Caption
                    Case Is = 2
                        Range("D" & UltimaLinha).Value = Me.Controls(ctlOpt.Caption).Caption
                    Case Is = 3
                        Range("E" & UltimaLinha).Value = Me.Controls(ctlOpt.Caption).Caption
                    Case Is = 4
                        Range("F" & UltimaLinha).Value = Me.Controls(ctlOpt.Caption).Caption
                End Select
                
            End If
        End If
    Next


Range("G" & UltimaLinha).Value = TextBox3.Text

Range("H" & UltimaLinha).Value = TextBox4.Text

lsLimparTextBox UserForm1
   
    MsgBox "Cadastro Realizado com Sucesso!", vbDefaultButton1, "CADASTRO DE DADOS"
    TextBox2.SetFocus


End Sub
 
Postado : 07/10/2019 3:12 pm
(@lzm11)
Posts: 0
New Member
Topic starter
 

Resolvido, muito obrigado!

 
Postado : 08/10/2019 7:34 am