Notifications
Clear all

Mesclar celulas ao cadastrar com um formulario.

9 Posts
2 Usuários
0 Reactions
952 Visualizações
(@berlan)
Posts: 13
Active Member
Topic starter
 

Boa noite,
Sou novo no forum, vim em busca de ajuda, pois sou novo em excel/vba, mais sempre que eu poder ajudarei também ...

Bom minha duvida é a seguinte, tenho um formulário com algumas textbox e uma listbox, ele cadastra dados dos dois na planilha, preciso que quando seja cadastrado, cada dado mescle com culunas do lado dentro da mesma linha, so preciso disso para estender a linha, abaixo vou postar as macros para ser adaptadas...

Uso a macro abaixo pra cadastrar...
_____________________________________________________________
Private Sub bt_gravar_Click()

Application.Run "desprotege"

Range(B2:C2).Select
Selection.Merge

Dim Nlin
Dim Cont

Sheets("v.produto").Select

Nlin = Range("B1000").End(xlUp).Row
Nlin = Nlin + 1

For Cont = 0 To Me.txt_carrinho.ListCount - 1
Plan14.Range("B" & Nlin) = txt_numeropedido.Text
Plan14.Range("D" & Nlin) = Me.txt_carrinho.List(Cont, 0)
Plan14.Range("F" & Nlin) = Me.txt_carrinho.List(Cont, 1)
Plan14.Range("L" & Nlin) = Me.txt_carrinho.List(Cont, 2)
Plan14.Range("P" & Nlin) = txt_cliente.Text
Plan14.Range("T" & Nlin) = txt_vendedor.Text
Plan14.Range("X" & Nlin) = Me.txt_carrinho.List(Cont, 3)
Plan14.Range("Z" & Nlin) = Me.txt_carrinho.List(Cont, 4)
Plan14.Range("AC" & Nlin) = Me.txt_carrinho.List(Cont, 5)
Plan14.Range("AF" & Nlin) = Me.txt_carrinho.List(Cont, 6)
Plan14.Range("AI" & Nlin) = Me.txt_carrinho.List(Cont, 7)
Plan14.Range("AL" & Nlin) = txt_datainclusão.Text
Nlin = Nlin + 1
Next

MsgBox "Venda efetuada com sucesso!"
End

Application.Run "protege"
End Sub
_____________________________________________________________

O problema é que cada cadastro tem muitas linhas, então não posso ja informa o numero dalinha da celula na macro, e em cada linha vai ter várias mesclagem diferentes em cada dado cadastrado...

o que eu estou tentando fazer é mais ou menos isso:
Range("B"linha atual:"C" linha atual).Select
Selection.Merge

bom, se alguem poder me ajudar, agradeço des de já!

 
Postado : 16/04/2013 8:35 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Bom dia!!

Poste seu arquivo modelo compactado!!!

Att

 
Postado : 17/04/2013 3:39 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Não sei se entendi corretamente, então vai um palpite :

Apos a linha :

Plan14.Range("B" & Nlin) = txt_numeropedido.Text
adicione a linha abaixo :
Plan14.Range("B" & Nlin & ":" & "C" & Nlin).Merge

Se não for isto, retorne.

[]s

 
Postado : 17/04/2013 5:28 am
(@berlan)
Posts: 13
Active Member
Topic starter
 

Consegui fazer isso com o codigo seguinte:

_____________________________________________
Private Sub bt_gravar_Click()

Application.Run "desprotege"

Dim Nlin
Dim Cont

Sheets("v.produto").Select

Nlin = Range("B1000").End(xlUp).Row
Nlin = Nlin + 1

Dim selecao As String

For Cont = 0 To Me.txt_carrinho.ListCount - 1
selecao = "B" & Nlin
selecao = selecao & ":C"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("B" & Nlin) = txt_numeropedido.Text
selecao = "D" & Nlin
selecao = selecao & ":E"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("D" & Nlin) = Me.txt_carrinho.List(Cont, 0)
selecao = "F" & Nlin
selecao = selecao & ":K"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlLeft
Plan14.Range("F" & Nlin) = Me.txt_carrinho.List(Cont, 1)
selecao = "L" & Nlin
selecao = selecao & ":O"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlLeft
Plan14.Range("L" & Nlin) = Me.txt_carrinho.List(Cont, 2)
selecao = "P" & Nlin
selecao = selecao & ":S"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlLeft
Plan14.Range("P" & Nlin) = txt_cliente.Text
selecao = "T" & Nlin
selecao = selecao & ":W"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlLeft
Plan14.Range("T" & Nlin) = txt_vendedor.Text
selecao = "X" & Nlin
selecao = selecao & ":Y"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("X" & Nlin) = Me.txt_carrinho.List(Cont, 3)
selecao = "Z" & Nlin
selecao = selecao & ":AB"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("Z" & Nlin) = Me.txt_carrinho.List(Cont, 4)
selecao = "AC" & Nlin
selecao = selecao & ":AE"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("AC" & Nlin) = Me.txt_carrinho.List(Cont, 5)
Selection.Style = "Currency"
selecao = "AF" & Nlin
selecao = selecao & ":AH"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("AF" & Nlin) = Me.txt_carrinho.List(Cont, 6)
Selection.Style = "Currency"
selecao = "AI" & Nlin
selecao = selecao & ":AK"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("AI" & Nlin) = Me.txt_carrinho.List(Cont, 7)
Selection.Style = "Currency"
selecao = "AL" & Nlin
selecao = selecao & ":AN"
selecao = selecao & Nlin
Range(selecao).Select
Selection.Merge

Selection.HorizontalAlignment = xlCenter
Plan14.Range("AL" & Nlin) = txt_datainclusão.Text
Nlin = Nlin + 1
Next

MsgBox "Venda efetuada com sucesso!"
End

Application.Run "protege"
End Sub
___________________________________________

Ta ai, se alguem precisar algum dia ... Brigadão pela ajuda de vocês, porem estou com outro problema....

Preciso de uma macro que some o valor de uma coluna de uma listbox e mostre o resultado em uma textbox, de preferencia some automatico, sem precisar clicar em um botão para fazer o calculo...

Espero que alguem possa me ajudar, e ja agradeço a todos que perderam um tempinho comigo...

 
Postado : 17/04/2013 10:05 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Berlan, como diz ser iniciante em VBa, meus parabens por conseguir chegar ao resultado esperado, com certeza esta sua rotina da para dar uma reduzida, simplificando algumas ações, por exemplo se ver na instrução que sugeri, não precisamos selecionar os ranges e depois efetuar a mesclagem, é só referenciarmos da forma que coloquei.
No momento estou aproveitando o finalzinho do almoço,e se der mais tarde dou uma lipada na mesma.

Quantpo a questão de somar no listbox, segue uma rotina utilizando a opção de um botão, mas a lógica é a mesma, é só colocar no evento do formulario dependendo de como está a sequencia e adaptar.

Soma os Valores do ListBox1, Segunda Coluna do ListBox e mostra o resultado em um TextBox:

Private Sub CommandButton2_Click()
Dim lItem As Double
Dim valor As Double

If TextBox1.Text = "" Then TextBox1.Text = 0
    For lItem = 0 To ListBox1.ListCount - 1
        valor = ListBox1.List(lItem, 1) * 1
        TextBox1.Text = (TextBox1.Text) + (valor)
    Next
End Sub

[]s

 
Postado : 17/04/2013 10:18 am
(@berlan)
Posts: 13
Active Member
Topic starter
 

Boa noite,
Mauro Coutinho, não estou usando sua macro para mesclar, pois não conseguir deixar o alinhamento dela do geito que eu quero, pois so alinha a primeira linha cadastrada, o resto das linhas do mesmo pedido não alinha...

E a macro de soma, não conseguir funfar ela, nem chamando ela por um botão, nem adicionando direto no formularios...

 
Postado : 17/04/2013 7:08 pm
(@berlan)
Posts: 13
Active Member
Topic starter
 

Consegui fazer a soma que eu queria, porem ainda estou com problema, uso a mesma macro que adiciona os valores no listbox para executar o codigo para fazer a soma, abaixo segue o codigo:

Private Sub addproduto_Click()
On Error Resume Next

txt_carrinho.AddItem txt_códigoproduto.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 1) = txt_produto.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 2) = txt_categoria.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 3) = txt_unidade.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 4) = txt_quantidade.Value
txt_carrinho.List(txt_carrinho.ListCount - 1, 5) = Format(txt_valor.Text, "R$ 0.00")
txt_carrinho.List(txt_carrinho.ListCount - 1, 6) = Format(txt_desconto.Text, "R$ 0.00")
txt_carrinho.List(txt_carrinho.ListCount - 1, 7) = Format(txt_valortotal.Text, "R$ 0.00")

txt_totalproduto.Text = "R$ 0,00"
txt_totalproduto.Text = Format(CDbl(txt_carrinho.List(0, 6)) + CDbl(txt_carrinho.List(0, 7)), "R$ 0.00")

txt_códigoproduto.Text = ""
txt_produto.Text = ""
txt_categoria.Text = ""
txt_unidade.Text = ""
txt_quantidade.Value = "0"
txt_desconto.Text = "0"
txt_valor.Text = Format(txt_valor.Text, "R$ 0.00")
txt_desconto.Text = Format(txt_desconto.Text, "R$ 0.00")
txt_valortotal.Text = Format(txt_valortotal.Text, "R$ 0.00")

End Sub

Problema dessa macro é que ele so soma o valor da primeira linha da listbox, o que posso fazer para essa macro somar todas as linhas ?

 
Postado : 17/04/2013 10:08 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Berlan, só poo : "E a macro de soma, não conseguir funfar ela, nem chamando ela por um botão, nem adicionando direto no formularios...", fica dificil dar um plapite certeiro, se ao menos colocasse se está dando algum erro ou qual o resultado, poderiamos tentar um chute, o ideal seria anexar um modelo compactado e reduzido para uma melhor analise, pois analisando só pelas instruções que postou vamos ficar dando palpites, e pode ser que a soma não esteja sendo efetuada devido aos valores carregados no listbox não estarem do tipo numerico.

De qq forma segue um exemplo onde tem algumas opções de Somar colunas de um Listbox, veja se ajuda.

Somar Colunas Listbox

[]s

 
Postado : 18/04/2013 5:38 am
(@berlan)
Posts: 13
Active Member
Topic starter
 

Boa tarde a todos,

consegui por pra funfar sua macro, coloquei ela pra rodar no proprio botão de add o produto, olha como ficou:

Private Sub addproduto_Click()
On Error Resume Next

txt_carrinho.AddItem txt_códigoproduto.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 1) = txt_produto.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 2) = txt_categoria.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 3) = txt_unidade.Text
txt_carrinho.List(txt_carrinho.ListCount - 1, 4) = txt_quantidade.Value
txt_carrinho.List(txt_carrinho.ListCount - 1, 5) = Format(txt_valor.Text, "R$ 0.00")
txt_carrinho.List(txt_carrinho.ListCount - 1, 6) = Format(txt_desconto.Text, "R$ 0.00")
txt_carrinho.List(txt_carrinho.ListCount - 1, 7) = Format(txt_valortotal.Text, "R$ 0.00")
txt_carrinho.List(txt_carrinho.ListCount - 1, 8) = Format(txt_totaltotal.Text, "R$ 0.00")

txt_totalproduto.Text = "R$ 0,00"
txt_valordesconto.Text = "R$ 0,00"
txt_valorfinal.Text = "R$ 0,00"

Dim valor As Double
Dim lItem As Double

If txt_totalproduto.Text = "" Then txt_totalproduto.Text = 0
For lItem = 0 To txt_carrinho.ListCount - 1
valor = txt_carrinho.List(lItem, 8) * 1
txt_totalproduto = Format(CCur(txt_totalproduto) + (valor), "R$0.00")
Next
If txt_valordesconto.Text = "" Then txt_valordesconto.Text = 0
For lItem = 0 To txt_carrinho.ListCount - 1
valor = txt_carrinho.List(lItem, 6) * 1
txt_valordesconto = Format(CCur(txt_valordesconto) + (valor), "R$0.00")
Next
If txt_valorfinal.Text = "" Then txt_valorfinal.Text = 0
For lItem = 0 To txt_carrinho.ListCount - 1
valor = txt_carrinho.List(lItem, 7) * 1
txt_valorfinal = Format(CCur(txt_valorfinal) + (valor), "R$0.00")
Next

txt_códigoproduto.Text = ""
txt_produto.Text = ""
txt_categoria.Text = ""
txt_unidade.Text = ""
txt_quantidade.Value = "0"
txt_desconto.Text = "0"
txt_valor.Text = Format(txt_valor.Text, "R$ 0.00")
txt_desconto.Text = Format(txt_desconto.Text, "R$ 0.00")
txt_valortotal.Text = Format(txt_valortotal.Text, "R$ 0.00")

End Sub

Se alguem precisar de uma macro parecida algum dia, ta ai! :]

Obrigadão mauro e a todos que pelomenos leram o topico!

OBS: caso tenha outra duvida, posso posta nessa mesmo tópico, mesmo sendo de outro assunto, ou abro outro??

 
Postado : 18/04/2013 10:27 am