Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
#34736
Bom dia, pessoal!!

Seguinte. Tenho alguns passos a serem executados durante uma venda. No momento, cada parte de codigo está separado em suas Subs. Mas descobri que tem como usar o GOTO para fazer esses passos. Qual vcs me recomendam utulizar. Segue exemplo dos dois tipos

UTILIZANDO O GOTO
Código: Selecionar todos
Sub Pagamento()

If Me.CmbPagamento.Value = "Dinheiro" Then
        GoTo Dinheiro
     
    ElseIf Me.CmbPagamento.Value = "Cartão" Then
        GoTo Cartão

Dinheiro:
Set Tabela = Plan3.ListObjects("Tab_Caixa")
Set Registro = Tabela.ListRows.Add

'Conjunto de Endereços""""""""""""""""""""""

With Registro

    .Range(1, 1).Value = CDate(Me.TxtData)
    .Range(1, 2).Value = frmVenda.TxtQtde.Value * 1
    .Range(1, 3).Value = frmVenda.CmbProduto.Value
End With

GoTo Fim
 
'---------------------------------------------------------------------------------------------
Cartão:
 Set Tabela = Plan6.ListObjects("Tab_Cartao")
Set Registro = Tabela.ListRows.Add

'Conjunto de Endereços""""""""""""""""""""""

With Registro
    .Range(1, 1).Value = CDate(Me.TxtData)
    .Range(1, 2).Value = frmVenda.TxtQtde.Value
    .Range(1, 3).Value = frmVenda.CmbProduto.Value
End With

GoTo Fim

Fim:
End Sub
Agora utilizando as Subs é o msmo codigo, mas cada tipo (Dinheiro e Cartao), estão em subs separadas
Código: Selecionar todos
Sub Pagamento()

If Me.CmbPagamento.Value = "Dinheiro" Then
        Call Dinheiro
     
    ElseIf Me.CmbPagamento.Value = "Cartão" Then
        Call Cartão
End Sub
Qual é a forma correta? Desde já agradeço a ajuda
#34738
Julgo que não é necessário utilizar a expressão GOTO, basta reestruturar o código
Código: Selecionar todos
Sub Pagamento()
    
    If Me.CmbPagamento.Value = "Dinheiro" Then
        Set Tabela = Plan3.ListObjects("Tab_Caixa")
        Set Registro = Tabela.ListRows.Add
        
        'Conjunto de Endereços""""""""""""""""""""""
        With Registro
            
            .Range(1, 1).Value = CDate(Me.TxtData)
            .Range(1, 2).Value = frmVenda.TxtQtde.Value * 1
            .Range(1, 3).Value = frmVenda.CmbProduto.Value
        End With
        
        
    ElseIf Me.CmbPagamento.Value = "Cartão" Then
        Set Tabela = Plan6.ListObjects("Tab_Cartao")
        Set Registro = Tabela.ListRows.Add
        
        'Conjunto de Endereços""""""""""""""""""""""
        
        With Registro
            .Range(1, 1).Value = CDate(Me.TxtData)
            .Range(1, 2).Value = frmVenda.TxtQtde.Value
            .Range(1, 3).Value = frmVenda.CmbProduto.Value
        End With
    End If

End Sub
Quanto a mim a segunda opção é mais limpa.
#34743
Código: Selecionar todos
Sub Pagamento()
 Dim Tabela As ListObject
  If Me.CmbPagamento.Value = "Dinheiro" Then
    Set Tabela = Plan3.ListObjects("Tab_Caixa")
  Else: Set Tabela = Plan6.ListObjects("Tab_Cartao")
  End If
   With Tabela.ListRows.Add
    .Range(1, 1).Value = CDate(Me.TxtData)
    .Range(1, 2).Value = frmVenda.TxtQtde.Value * 1
    .Range(1, 3).Value = frmVenda.CmbProduto.Value
   End With
End Sub
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord