Somar Listbox com condição

Guima

18-04-2010 23:19:55

Boa noite Pessoal,

Embora pareça simples eu acabei me complicando (além de ter muito tempo que não faço) com a soma de algumas colunas do Listbox que dependem de algumas condições. Eu preciso somar todas os itens da 4ª coluna Listbox e todos os itens da 5ª coluna, que nem sempre terão dados. Criei a rotina abaixo, mas apesar de somar, tá sempre somando o valor que já se encontra no Duplicando o valor que já se encontra no TEXT, mais o novo valor.

Código: Selecionar todos

Sub Carrega()

Dim valor As Double
Dim PROV As Double
Dim DESC As Double
Dim Acum As Double

PROV = 0
DESC = 0
Acum = 0

'sempre coloca o algarismo 0 em caso de "" para efetivar a soma
If totalPROV = "" Then
totalPROV = 0
End If

    If totalDESC = "" Then
    totalDESC = 0
    End If

        If txtvalor = "" Then
        txtvalor = 0
        End If

valor = txtvalor
PROV = totalPROV
DESC = totalDESC

'verifica se tem funcionário selecionado
If txtsal = "" Then
    MsgBox "Informe o funcionário", vbInformation, "MiG"
    Exit Sub
End If
   
   
'If txtvbase = "" And txtpercent = "" Then
    'txtvalor = txtsal
'End If

'impede que valores zerados passem
If txtvalor = "" Then
        MsgBox "Informe o valor ou passe para próxima verba", vbInformation, "MiG"
    Exit Sub
End If


Dim i As Double

i = 0

    ' alimenta o LIstbox
    Me.ListBox1.AddItem , ListIndex
    Me.ListBox1.List(i, 0) = txtcod
    Me.ListBox1.List(i, 1) = txtdescri
    Me.ListBox1.List(i, 2) = txtref
            'soma a coluna PROVENTOS
        If txtprov = "1" Then
            Me.ListBox1.List(i, 3) = Format(txtvalor, "R$ #,##0.00")
            PROV = totalPROV + valor
                If ListBox1.List(i, 3) <> "" Then
                    totalPROV = PROV + totalPROV
                End If
        Else
            'soma a coluna DESCONTOS
            Me.ListBox1.List(i, 4) = Format(txtvalor, "R$ #,##0.00")
            DESC = totalDESC + valor
                If ListBox1.List(i, 4) <> "" Then
                    totalDESC = totalDESC + DESC
                End If
            
        End If
 
' chama rotina para ir para a próxima verba
PROXIMO
txtref.SetFocus
End Sub

webmaster

20-04-2010 17:14:52

Guima,

Pode disponibilizar a planilha? Ou pelo menos a parte que faz isso. Vai ser mais fácil ajudar.

Abraços

Guima

21-04-2010 12:07:42

Olá Tomas,

Agradeço sua atenção, mas já consegui resolver o problema. Eu não havia criado uma variável para armazenar os valores, dessa forma, todo novo registrado substituía o anterior.

Abraço