Ir para conteúdo
Fórum Script Brasil
  • 0

VBA Para cadastro com error de compilação: O objeto With precisa ser de tipo definido pelo usuário, Object ou Variant


Yago bezerra

Pergunta

6 respostass a esta questão

Posts Recomendados

  • 0

Fiz a alteração trocando 

"reg_auditor.close" para "Unload reg_auditor"

sobre o exemplo acredito que não tem um exemplo direto pois é um sistema de gerenciamento de 5S que estou tentando criar na empresa, o mais próximo disso seria um formulário de cadastro com userform.

eu sou novo aqui(até em forums haha), então descupe por esses erros.

excel não consegui postar, mas vou deixar o código em texto.

 


Sub B_cadastrarauditor_Click()

Dim audit_off As Object
Dim audit_man As Object
Dim a_man As Object
Dim a_off As Object
Dim TXT_auditorname As MSForms.TextBox
Dim OB_off As MSForms.OptionButton
Dim OB_man As MSForms.OptionButton
Dim ws As Worksheet
Dim nextRow As Long

If OB_off.Enabled And OB_man.Enabled Then
    With OB_off
    MsgBox "Selecione em qual área será cadastrado o auditor"
    End With
End If

    
If TXT_auditorname = "" Then
        MsgBox "Preencha o nome do auditor"

    ElseIf OB_off = True Then
        Set ws = ThisWorkbook.Sheets("Database_tables")
    
        'Get the next empty row in the table
        nextRow = ws.ListObjects("audit_off").Range.Rows.Count + 1
    
        'Capture data from the UserForm text box
        ws.ListObjects("audit_off").ListRows.Add.Range(1, 1).Value = TXT_auditorname.Value
    
        'Clear the UserForm text box after data is captured
        TXT_auditorname.Value = ""
        
        MsgBox "Auditor cadastrado com sucesso"
        
    ElseIf OB_man = True Then
        Set ws = ThisWorkbook.Sheets("Database_tables")
    
        'Get the next empty row in the table
        nextRow = ws.ListObjects("audit_man").Range.Rows.Count + 1
    
        'Capture data from the UserForm text box
        ws.ListObjects("audit_man").ListRows.Add.Range(1, 1).Value = TXT_auditorname.Value
    
        'Clear the UserForm text box after data is captured
        TXT_auditorname.Value = ""
        
        MsgBox "Auditor cadastrado com sucesso"
        
End If

Unload reg_auditor

    
End Subimage.png.7cf0f4c333039b79b22a0c4c72f1e42c.png

Link para o comentário
Compartilhar em outros sites

  • 0

Ouve altrações no código em relação ao topíco anterior

Atual:

If OB_off.Enabled And OB_man.Enabled Then
    With OB_off
    MsgBox "Selecione em qual área será cadastrado o auditor"
    End With
End If

 

Anterior: 

If OB_off = False And OB_man = False Then
        MsgBox "Selecione em qual área será cadastrado o auditor"
        Exit Sub
    ElseIf TXT_auditorname = "" Then
        MsgBox "Preencha o nome do auditor"
        Exit Sub
    End If

Para verificar se um OptionButton foi clicado, mantenha esse trecho do codigo, anterior.

O erro gerado, é porque dclarou as variaveis: 

Dim OB_off As MSForms.OptionButton
Dim OB_man As MSForms.OptionButton

E não "setou" 

 

* Para anexar algum a arquivo, faça upload em algum site na nuvem, como por exemplo google drive, e poste o link aqui.

 

 

 

Link para o comentário
Compartilhar em outros sites

  • 0

Corrigido! deu certo.

inseri um exit sub para que  não ocorra o preenchimento de celula vazia.

Obrigado!. segue código para uso da comunidade

 

Sub B_cadastrarauditor_Click()

Dim TXT_auditorname As MSForms.TextBox
Set TXT_auditorname = Me.TXT_auditorname
Dim ws As Worksheet
Dim OB_off As MSForms.OptionButton
Set OB_off = Me.OB_off
Dim OB_man As MSForms.OptionButton
Set OB_man = Me.OB_man
Dim nextRow As Long
    

    If OB_off = False And OB_man = False Then
        MsgBox "Selecione em qual área será cadastrado o auditor"
        Exit Sub
    ElseIf TXT_auditorname = "" Then
        MsgBox "Preencha o nome do auditor"
        Exit Sub
    End If

    
    If OB_off = True Then
        Set ws = ThisWorkbook.Sheets("Database_tables")
    
        'Get the next empty row in the table
        nextRow = ws.ListObjects("audit_off").Range.Rows.Count + 1
    
        'Capture data from the UserForm text box
        ws.ListObjects("audit_off").ListRows.Add.Range(1, 1).Value = TXT_auditorname.Value
    
        'Clear the UserForm text box after data is captured
        TXT_auditorname.Value = ""
        
        MsgBox "Auditor cadastrado com sucesso"
        
    ElseIf OB_man = True Then
        Set ws = ThisWorkbook.Sheets("Database_tables")
    
        'Get the next empty row in the table
        nextRow = ws.ListObjects("audit_man").Range.Rows.Count + 1
    
        'Capture data from the UserForm text box
        ws.ListObjects("audit_man").ListRows.Add.Range(1, 1).Value = TXT_auditorname.Value
    
        'Clear the UserForm text box after data is captured
        TXT_auditorname.Value = ""
        
        MsgBox "Auditor cadastrado com sucesso"
        
    End If

    Unload reg_auditor
        
End Sub

Link para o comentário
Compartilhar em outros sites

  • 0

Complementando...

Todo esse trecho do código, abaixo, é desnecessario, caso os componentes (TextBox, OptionButton, etc), tenham o mesmos nomes:

Dim TXT_auditorname As MSForms.TextBox
Set TXT_auditorname = Me.TXT_auditorname
Dim OB_off As MSForms.OptionButton
Set OB_off = Me.OB_off
Dim OB_man As MSForms.OptionButton
Set OB_man = Me.OB_man


 

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...