Jump to content

[Resolvido] Inserir dados do Form na Folha de Excel


gregurs

Recommended Posts

Boa noite, tenho um formulário de fornecedores no qual consigo inserir na folha de excel o meu problema é que na folha de excel ele só me insere na mesma linha.

o programa deve na folha de escel começar a inserir registos a partir da linha 12

o codigo que tenho é este:

totalregistro = Worksheets("TABFORNECEDORES").UsedRange.Rows.Count + 1
With Worksheets("TABFORNECEDORES")
.Cells(totalregistro, 2) = numerofornecedor
.Cells(totalregistro, 3) = nome
.Cells(totalregistro, 4) = contribuinte
.Cells(totalregistro, 5) = endereço
.Cells(totalregistro, 6) = contactofixo
.Cells(totalregistro, 7) = contactomovel
.Cells(totalregistro, 8) = email
End With
MsgBox "Gravado com sucesso"
Link to comment
Share on other sites

Boas gregurs

Esperimente o seguinte procedimento num form à parte para ver se te serve.

Private sub Cmd_Inserir()
Dim TotalLinhas As Long
Dim n1 As Variant

'Verifica quantas linhas tem a coluna.
TotalLinhas = Range("A:A").Count
'No Range indicas a faixa a ser procurada.

'Varre a coluna "A" de baixo para cima.
Range("A+Cstr(TotalLinhas)).End(xlUp).select

'Inserindo dados.
n1 = InputBox"Entre com um Valor"

'Desloca-se para a próxima célula vasia ativando-a e transferindo o valor para ela.
ActiveCell.Offset(1,0).Value = Cdbl(n1)

End Sub

Toda vez que premir o botão inserir, você acrescentará um valor na próxima célula.

Veja se esta sugestão te ajuda.

Edison

Link to comment
Share on other sites

  • 3 weeks later...

obrigado pela solução mas não consegui nada entre tanto consegui fazer isto mas o problema mantem-se pode ser que me detem algum problema no codigo

linha = Worksheets("TABFORNECEDORES").UsedRange.Rows.Count + 1
For linha = 9 To 20
  linha = linha + 1
With Worksheets("TABFORNECEDORES")
.Cells(linha, 2) = numerofornecedor
.Cells(linha, 3) = nome
.Cells(linha, 4) = contribuinte
.Cells(linha, 5) = endereço
.Cells(linha, 6) = contactofixo
.Cells(linha, 7) = contactomovel
.Cells(linha, 8) = email
End With
linha = linha + 1
Exit For
Next linha
Link to comment
Share on other sites

Eu uso o seguinte código para fazer algo semelhante e funciona bem.

Private Sub AdicionarCliente_Click()
Dim NewRow As Integer
Dim ws As Worksheet
Set ws = Worksheets("Tabelas Aux")
NewRow = Worksheets("Tabelas Aux").Range("B" & Rows.Count).End(xlUp).Row + 1

If Trim(Me.TextBox1.Value) = "" Then
 Me.TextBox1.SetFocus
 MsgBox "Por favor introduza nome do cliente"
 Exit Sub
End If
'copy the data to the database
Worksheets("tabelas aux").Cells(NewRow, 2).Value = Addcliente.TextBox1.Value

Me.TextBox1.Value = ""
Addcliente.Hide

End Sub
Link to comment
Share on other sites

  • 5 weeks later...

boas

estive ausente só hoje tive oportunidade.

linha = Worksheets("TABFORNECEDORES").UsedRange.Rows.Count + 1

isto acima não faz nada porque no contador «for» estás a dar o valor 9 á linha.

For linha = 9 To 20
  linha = linha + 1
With Worksheets("TABFORNECEDORES")
   .Cells(linha, 2) = numerofornecedor
   .Cells(linha, 3) = nome
   .Cells(linha, 4) = contribuinte
   .Cells(linha, 5) = endereço
   .Cells(linha, 6) = contactofixo
   .Cells(linha, 7) = contactomovel
   .Cells(linha, 8) = email
End With
   linha = linha + 1
Exit For
Next linha

aqui o exit for é o teu problema termina o contador, elimina-o, e a instrução linha = linha + 1 estate a fazer andar o contador 2 linha em vez de 1, deves apagar as duas instruções, que o contador aumenta uma linha automaticamente.

e como deste o valor 9 á linha vai começar na linha 9 teras que mudar para 12.

cumps

acao

Link to comment
Share on other sites

Boa noite, tenho um formulário de fornecedores no qual consigo inserir na folha de excel o meu problema é que na folha de excel ele só me insere na mesma linha.

o programa deve na folha de escel começar a inserir registos a partir da linha 12

o codigo que tenho é este:

totalregistro = Worksheets("TABFORNECEDORES").UsedRange.Rows.Count + 1
With Worksheets("TABFORNECEDORES")
.Cells(totalregistro, 2) = numerofornecedor
.Cells(totalregistro, 3) = nome
.Cells(totalregistro, 4) = contribuinte
.Cells(totalregistro, 5) = endereço
.Cells(totalregistro, 6) = contactofixo
.Cells(totalregistro, 7) = contactomovel
.Cells(totalregistro, 8) = email
End With
MsgBox "Gravado com sucesso"

boas

estive a testar o teu código,

altera para

totalregistro = Worksheets("TABFORNECEDORES").UsedRange.Rows.Count + 2

funciona em pleno.

cumps

acao

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.