Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50090

subscripts out of range

$
0
0
Hi there,

I have the code written, but when I run it I get "subscriptso ut of range" problem.

Code:

Option Explicit

Sub keepLast()
Dim lastrows1 As Long
Dim lastrows2 As Long

Dim b As Boolean
Dim c As Boolean

Dim r As Long
Dim k As Long

Worksheets("Last Units on 59 N").Activate
Worksheets("Last Units on 59 N").Cells(1, 8).Value = "Status"

lastrows1 = Worksheets("Last Units on 59 N").UsedRange.Rows.Count

    For r = 2 To lastrows1
    If Cells(r, 1) = Cells(r + 1, 1) Then
    b = True
        If b = True Then
        Cells(r, 8).Value = "delete"
        Cells(r + 1, 8).Value = "keep"
        End If
    End If
   
    Next r
   
    For r = 2 To lastrows1
        If Cells(r, 8).Value = "" Then
            Cells(r, 8).Value = "keep"
        End If
        Next r
       
       
Sheets("Last Units on 59 S").Activate
Worksheets("Last Units on 59 S").Cells(1, 8).Value = "Status"

lastrows2 = Worksheets("Last Units on 59 S").UsedRange.Rows.Count

    For k = 2 To lastrows2
    If Cells(k, 1) = Cells(k + 1, 1) Then
    b = True
        If b = True Then
        Cells(k, 8).Value = "delete"
        Cells(k + 1, 8).Value = "keep"
        End If
    End If
   
    Next k
   
    For k = 2 To lastrows2
        If Cells(k, 8).Value = "" Then
            Cells(k, 8).Value = "keep"
        End If
        Next k
           
       
End Sub

The error seems to be on the first worksheets statement.

any help is much appreciated.

Thanks

Viewing all articles
Browse latest Browse all 50090

Trending Articles