0

I've been working on a macro in Excel that sorts through a table in one sheet ("Minor") and when a criteria is met it should add data from that table to another one in a second sheet ("Sheet1"), but I keep running with the 1004 error "object not defined" and I don't understand why, please help. Thanks!

Sub ord_esp_aprob()
  a = Worksheets("Minor").Cells(Rows.Count, 1).End(xlUp).Row
  b = Worksheets("Sheet1").Cells(Rows.Count, 4).End(xlUp).Row + 1

  For i = 3 To a
    If Worksheets("Minor").Cells(i, 1).Value = "Orden en Espera de Aprobación" Then
      Worksheets("Sheet1").Range(Cells(b, 4)).Value = Worksheets("Minor").Range(Cells(i, 2)).Value '(This is where the error occurs)
      Worksheets("Sheet1").Activate
    End If
  Next i
End Sub
Community
  • 1
  • 1

1 Answers1

0

You're missing range references when setting the value of:

Worksheets("Sheet1").Range(Cells(b, 4)).Value

Try this:

Option Explicit

Sub ord_esp_aprob()

Dim a As Long
Dim b As Long
Dim i As Long

Dim sht_minor As Worksheet
Dim sht_sht1 As Worksheet

Set sht_minor = ThisWorkbook.Worksheets("Minor")
Set sht_sht1 = ThisWorkbook.Worksheets("Sheet1")

a = sht_minor.Cells(sht_minor.Rows.Count, 1).End(xlUp).Row
b = sht_sht1.Cells(sht_sht1.Rows.Count, 4).End(xlUp).Row + 1

For i = 3 To a

If sht_minor.Cells(i, 1).Value = "Orden en Espera de Aprobación" Then
    sht_sht1.Cells(b, 4).Value = sht_minor.Cells(i, 2).Value
    sht_sht1.Activate
End If

Next i

End Sub
warner_sc
  • 848
  • 6
  • 13