0

I need to start at the top of column L then go down row by row until the last row.

  • If the value in column L = 8 then copy the value from column E into column A.

  • If the value of column L = 7 then copy the value from column E into column B.

The error I receive is:

Method Range of Object_Global failed (1004)


    Sub CopyVals()

    Dim lngLoop As Long, lngRow As Long

    Do Until IsEmpty(ActiveCell)
        If Range("L" & lngLoop).Value = 8 Then
            Range("E" & lngLoop).Copy Range("A" & lngLoop)

        ElseIf Range("L" & lngLoop).Value = 7 Then
            Range("E" & lngLoop).Copy Range("B" & lngLoop)
        End If
        ActiveCell.Offset(1, 0).Select
    Loop

    End Sub
1
  • lngLoop never gets assigned any value so will always be zero. There is no such cell as L0 so there will be an error on the first pass through the loop. lngRow never gets used at all. You can do the whole thing with offsets so you probably don't need either of those variables
    – barrowc
    Mar 19, 2015 at 3:03

1 Answer 1

1
Sub CopyVals()

Dim lngLoop As Long

lngLoop = 1

For lngLoop = 1 To Rows.Count
  If Cells(lngLoop, 12).Value = "8" Then Cells(lngLoop, 1).Value = Cells(lngloop, 5).Value   

  If Cells(lngLoop, 12).Value = "7" Then Cells(lngLoop, 2).Value = Cells(lngloop, 5).Value

Next lngLoop
End Sub
1
  • Barrowc, Thank you so much for the help. It worked perfectly and makes a lot more sense then the direction I was going toward. It is very much appreciated.
    – DV Kayar
    Mar 19, 2015 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.