ajuda com codigo,gravar em duas panilhas diferentes

pedro_mexico

18-04-2018 11:22:11

bom dia amigos.

tenho um formulario em que introduzo o historico das viaturas da empresa.
o formulario grava os dados em uma panilha inicial , selecionamos uma combobox com a matricula da viatura e ele vai fazer gravar na linha certa da panilha.
mas agora eu queria criar uma panilha de historico para cada matricula e ja coloquei um codigo que envio em baixo,

Código: Selecionar todos

Private Sub botao_gravar_Click()

Dim linha As Object

Select Case cboMatricula.Value 'Selecione de acordo o valor de ComboBox
 
 Case Is = "XP-91-52"
            ThisWorkbook.Sheets("Mapa Viaturas").Select
             
              Set linha = Folha3.Range("C1") '.End(xlUp)

              If Not TextBox1.Text = vbNullString Then 'codigo para se a textbox estiver vazia nao alterar dados na folha
               linha.Offset(1, 1).Value = TextBox1.Text 'grava na (linha,  coluna)'0
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(1, 2).Value = TextBox2.Text '1
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(1, 10).Value = TextBox3.Text '9
                End If
              
              linha.Offset(1, 17).Value = cmb_pneus '16
              
              If Not TextBox4.Text = vbNullString Then
               linha.Offset(1, 18).Value = TextBox4.Text '17
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(1, 19).Value = TextBox5.Text '18
                End If
              
                   If CheckBox1.Value = True Then
                    linha.Offset(1, 15).Value = "SIM" '14
                    'Range("Q4").Interior = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If
                            
                            
    Select Case cboMatricula.Value
Case Is = "XP-91-52"
      ThisWorkbook.Sheets("XP-91-52").Select 'Informe entre aspas "" o nome da folha correspondente
          
End Select

   lin = ActiveSheet.UsedRange.Rows.Count + 1 'Define a variavel Lin como a ultima linha e soma + 1
   Cells(lin, "B") = TextBox1 'Primeira linha em branco na coluna A = txtapelido
   Cells(lin, "C") = TextBox2 'Primeira linha em branco na coluna B = txtnii1
   Cells(lin, "D") = TextBox3 'E assim por diante até o último campo do form
   Cells(lin, "F") = cmb_pneus
Cells(lin, "G") = TextBox4
Cells(lin, "H") = TextBox5
Cells(lin, "E") = TextBox6
Cells(lin, "I") = TextBox7
                            
                            
  Case Is = "12-82-GL"
            ThisWorkbook.Sheets("Mapa Viaturas").Select
        
              Set linha = Folha3.Range("C1")

              If Not TextBox1.Text = vbNullString Then
               linha.Offset(2, 1).Value = TextBox1.Text
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(2, 2).Value = TextBox2.Text
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(2, 10).Value = TextBox3.Text
                End If
              linha.Offset(2, 17).Value = cmb_pneus
              
              If Not TextBox4.Text = vbNullString Then
              linha.Offset(2, 18).Value = TextBox4.Text
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(2, 19).Value = TextBox5.Text
                End If
              
              
              If CheckBox1.Value = True Then
                    linha.Offset(2, 15).Value = "SIM"
                    'Range("Q5").Interior.Color = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If
              
              
  Select Case cboMatricula.Value
Case Is = "12-82-GL"
      ThisWorkbook.Sheets("12-82-GL").Select 'Informe entre aspas "" o nome da folha correspondente
          
End Select

   lin = ActiveSheet.UsedRange.Rows.Count + 1 'Define a variavel Lin como a ultima linha e soma + 1
   Cells(lin, "B") = TextBox1 'Primeira linha em branco na coluna A = txtapelido
   Cells(lin, "C") = TextBox2 'Primeira linha em branco na coluna B = txtnii1
   Cells(lin, "D") = TextBox3 'E assim por diante até o último campo do form
   Cells(lin, "F") = cmb_pneus
Cells(lin, "G") = TextBox4
Cells(lin, "H") = TextBox5
Cells(lin, "E") = TextBox6
Cells(lin, "I") = TextBox7
              
              
Case Is = "18-45-FH"
            ThisWorkbook.Sheets("Mapa Viaturas").Select
        
              Set linha = Folha3.Range("C1")

              If Not TextBox1.Text = vbNullString Then
               linha.Offset(3, 1).Value = TextBox1.Text
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(3, 2).Value = TextBox2.Text
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(3, 10).Value = TextBox3.Text
                End If
              linha.Offset(3, 17).Value = cmb_pneus
              
              If Not TextBox4.Text = vbNullString Then
              linha.Offset(3, 18).Value = TextBox4.Text
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(3, 19).Value = TextBox5.Text
                End If
              
              
              If CheckBox1.Value = True Then
                    linha.Offset(3, 15).Value = "SIM"
                    'Range("Q5").Interior.Color = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If
              
    Select Case cboMatricula.Value
Case Is = "18-45-FH"
      ThisWorkbook.Sheets("18-45-FH").Select 'Informe entre aspas "" o nome da folha correspondente
          
End Select

   lin = ActiveSheet.UsedRange.Rows.Count + 1 'Define a variavel Lin como a ultima linha e soma + 1
   Cells(lin, "B") = TextBox1 'Primeira linha em branco na coluna A = txtapelido
   Cells(lin, "C") = TextBox2 'Primeira linha em branco na coluna B = txtnii1
   Cells(lin, "D") = TextBox3 'E assim por diante até o último campo do form
   Cells(lin, "F") = cmb_pneus
Cells(lin, "G") = TextBox4
Cells(lin, "H") = TextBox5
Cells(lin, "E") = TextBox6
Cells(lin, "I") = TextBox7
              
              
Case Is = "57-24-TD"
           ThisWorkbook.Sheets("Mapa Viaturas").Select
        
              Set linha = Folha3.Range("C1")

              If Not TextBox1.Text = vbNullString Then
               linha.Offset(4, 1).Value = TextBox1.Text
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(4, 2).Value = TextBox2.Text
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(4, 10).Value = TextBox3.Text
                End If
              linha.Offset(4, 17).Value = cmb_pneus
              
              If Not TextBox4.Text = vbNullString Then
              linha.Offset(4, 18).Value = TextBox4.Text
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(4, 19).Value = TextBox5.Text
                End If
              
              
              If CheckBox1.Value = True Then
                    linha.Offset(4, 15).Value = "SIM"
                    'Range("Q5").Interior.Color = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If

Case Is = "XX-XX-XX"
            ThisWorkbook.Sheets("Mapa Viaturas").Select
        
              Set linha = Folha3.Range("C1")

              If Not TextBox1.Text = vbNullString Then
               linha.Offset(5, 1).Value = TextBox1.Text
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(5, 2).Value = TextBox2.Text
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(5, 10).Value = TextBox3.Text
                End If
              linha.Offset(5, 17).Value = cmb_pneus
              
              If Not TextBox4.Text = vbNullString Then
              linha.Offset(5, 18).Value = TextBox4.Text
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(5, 19).Value = TextBox5.Text
                End If
              
              
              If CheckBox1.Value = True Then
                    linha.Offset(5, 15).Value = "SIM"
                    'Range("Q5").Interior.Color = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If
              
Case Is = "70-EE-12"
           ThisWorkbook.Sheets("Mapa Viaturas").Select
        
              Set linha = Folha3.Range("C1")

              If Not TextBox1.Text = vbNullString Then
               linha.Offset(6, 1).Value = TextBox1.Text
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(6, 2).Value = TextBox2.Text
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(6, 10).Value = TextBox3.Text
                End If
              linha.Offset(6, 17).Value = cmb_pneus
              
              If Not TextBox4.Text = vbNullString Then
              linha.Offset(6, 18).Value = TextBox4.Text
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(6, 19).Value = TextBox5.Text
                End If
              
              
              If CheckBox1.Value = True Then
                    linha.Offset(6, 15).Value = "SIM"
                    'Range("Q5").Interior.Color = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If
              
Case Is = "52-GH-12"
           ThisWorkbook.Sheets("Mapa Viaturas").Select
        
              Set linha = Folha3.Range("C1")

              If Not TextBox1.Text = vbNullString Then
               linha.Offset(7, 1).Value = TextBox1.Text
                End If
              If Not TextBox2.Text = vbNullString Then
               linha.Offset(7, 2).Value = TextBox2.Text
                End If
              If Not TextBox3.Text = vbNullString Then
               linha.Offset(7, 10).Value = TextBox3.Text
                End If
              linha.Offset(7, 17).Value = cmb_pneus
              
              If Not TextBox4.Text = vbNullString Then
              linha.Offset(7, 18).Value = TextBox4.Text
                End If
              If Not TextBox5.Text = vbNullString Then
               linha.Offset(7, 19).Value = TextBox5.Text
                End If
              
              If CheckBox1.Value = True Then
               linha.Offset(7, 15).Value = "SIM"
               ' Range("Q5").Interior.Color = vbWhite
                      Else
                         'linha.Offset(3, 14).Value = "NÂO"
                            End If
              
     End Select
     
     MsgBox ("Gravado com Sucesso")
     
     TextBox1.Value = Empty
     TextBox2.Value = Empty
     TextBox3.Value = Empty
     TextBox4.Value = Empty
     TextBox5.Value = Empty
     cmb_pneus = Empty
     
MultiPage1.Value = 0

End Sub
para fazer gravacao dos dados na panilha inicial e numa panilha com a matricula da viatura, mas para a primeira viatura do codigo, ele faz a gravaçao bem, mas para as outras viaturas ele so grava na panilha inicial.
espero que me consigam dar umas ideias.
obrigado

webmaster

18-04-2018 14:16:42

Pedro,

Por favor, tente ser mais consistente no titulo de suas perguntas. "ajuda com codigo que dá erro" nao ajuda em nada a quem precisa se interessar pelo topico.

srobles

20-04-2018 18:47:32

pedro,

Veja se as alterações feitas em seu modelo atendem sua demanda. Favor, não exclua ou renomeie a aba Historico_Modelo, pois ela servirá como modelo para as novas planilhas, explico :

Ao clicar em gravar, se a aba da viatura selecionada não existir, a aba Historico_Modelo é copiada e essa nova cópia recebe o nome da viatura (matricula). Então nessa nova aba são copiados e colados os dados da viatura selecionada nela.

Espero ter ajudado.

Abs

pedro_mexico

24-04-2018 09:18:17

bom dia Srobles,
obrigado pela resposta .
a ideia é boa, ele criar uma pasta nova, mas o codigo nao esta a fazer gravaçao em nenhuma das pastas, nem na panilha "mapa viaturas! nem na nova panilha que criou.
fui confirmar no codigo se estava a gravar nas linhas indicadas e está correcto, mas nao grava

srobles

24-04-2018 15:07:21

pedro,

Fiz os teste aqui e correu tudo como programado. :?:
A gravação dos dados tanto na folha MAPA VIATURAS, quanto a folha da própria viatura receberam os dados corretamente. Veja :
Mapa Viaturas :
img1.png
Folha da Viatura :
img2.png
Estranho não ter ocorrido o mesmo em seus testes.

Abs

pedro_mexico

25-04-2018 18:31:11

boa tarde amigo Srobles.
eu aqui nao consigo gravar dados, ele cria a panilha nova com a matricula da viatura, mas nao grava nada, nem na panilha "mapa viaturas" nem na nova panilha com a matricula da viatura