0

I have a format that when its filled and a button is pressed it's supposed to sum a different range of cells and replace them. But i cant get it to work. I think its the references.

Private Sub C2_Click()
Dim Partida As String
Dim Rng As Range, Pacas As Range, Kilos As Range, Devol As Range, Desp As Range

If Sheets("Reporte").Range("C4").Value <> "Blanco" Then

'------------------> Color
    Partida = Worksheets("Reporte").Range("C3").Value
    If Trim(Partida) <> "" Then
            With Sheets("Color").Rows("6:6")
                Set Rng = .Find(What:=Partida, After:=.Cells(.Cells.Count), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)

                If Not Sheets("Reporte").Range("C5") Is Nothing Then
                    Set Pacas = Rng.Offset(16, 0)
                    Sheets("Color").Range(Pacas).Value = Sheets("Color").Range(Pacas).Value + Sheets("Reporte").Range("C5").Value
                Else: GoTo K
                End If

K:
                If Not Sheets("Reporte").Range("C6") Is Nothing Then
                    Set Kilos = Rng.Offset(17, 0)
                    Sheets("Color").Range(Kilos).Value = Sheets("Color").Range(Pacas).Value + Sheets("Reporte").Range("C5").Value
                Else: GoTo D
                End If
D:
                If Not Sheets("Reporte").Range("C7") Is Nothing Then
                    Set Devol = Rng.Offset(18, 0)
                    Sheets("Color").Range(Devol).Value = Sheets("Color").Range(Pacas).Value + Sheets("Reporte").Range("C5").Value
                Else: GoTo De:
                End If
De:
                If Not Sheets("Reporte").Range("C8") Is Nothing Then
                    Set Desp = Rng.Offset(19, 0)
                    Sheets("Color").Range(Desp).Value = Sheets("Color").Range(Pacas).Value + Sheets("Reporte").Range("C5").Value
                Else: GoTo C
                End If
C:
            End With


    Else
     PartidaN = MsgBox("Agregar partida", vbCritical, "Error")
    End If
Else
'------------------> Blanco
End If

End Sub

Error 1004 on the sum lines.

Armas.Chuy
  • 17
  • 8
  • 2
    If `Pacas` is a `Range`, then `Range(Pacas)` is not valid syntax. I'm guessing you want `Pacas.Value`? Same for `Kilos`, `Devol` and `Desp`. – BigBen Jul 08 '19 at 16:17
  • Also try to avoid using `GoTo`. It makes for spaghetti code. – BigBen Jul 08 '19 at 16:19
  • I think your `If` statements will always be true too. Did you mean to check `If Not Rng Is Nothing`? – BigBen Jul 08 '19 at 16:24
  • Heyy :D Yeah i need the value but in the range that u had, and its dif for Pacas Kilos etc. And no, if statements might be false since there might not be any info. – Armas.Chuy Jul 08 '19 at 16:33
  • That's not how you check if a `Range` is empty. Try [this approach](https://stackoverflow.com/questions/13360651/excel-how-to-check-if-a-cell-is-empty-with-vba) instead. – BigBen Jul 08 '19 at 16:42

0 Answers0