0

Code was working fine up until a couple of days ago but now getting the subject-line error. Help?

Sub CopyRows()
    Dim bottomL As Integer
    Dim x As Integer
        bottomL = Sheets("Pacer").Range("L" & Rows.Count).End(xlUp).Row: x = 1

    Dim c As Range
    For Each c In Sheets("Pacer").Range("A1:L" & bottomL)
        If (c.Value = "AFSB" Or c.Value = "TEIGIP4T" Or c.Value = "EPP") Then
            Intersect(c.Parent.Columns("A:Q"), c.EntireRow).Copy Worksheets("Portfolio").Range("A" & x + 1)
            x = x + 1
        End If
    Next c

End Sub
8
  • Which line actually produces the error? Commented Nov 8, 2017 at 13:19
  • If (c.Value = "AFSB" Or c.Value = "TEIGIP4T" Or c.Value = "EPP") Then Commented Nov 8, 2017 at 13:19
  • You've probably got a cell that appears as #######? Try using: If (c.Value2 = "AFSB" Or c.Value2 = "TEIGIP4T" Or c.Value2 = "EPP") Then Commented Nov 8, 2017 at 13:20
  • You are absolutely right about the formatting issue where a few cells appear as #######. Unfortunately, the correction you recommended still produces the Runtime error, but the code works when I manually override the formatting issue. Is the easiest thing to insert code that would allow me to format the data as it is being copied over? Commented Nov 8, 2017 at 13:27
  • Are those cells formatted as Date? If so, the correction I suggested has never failed me before - are you sure you changed all the Value to Value2? Commented Nov 8, 2017 at 13:30

2 Answers 2

1

Variable bottomL As Integer

Will give overflow error the moment it exceeds 32,767 rows. Try declaring it as long

bottomL As Long

Edit: The rule applies to X as well as it is incrementing.

Sign up to request clarification or add additional context in comments.

4 Comments

Gave that a shot, but still running into the overflow error. Not sure what I am doing wrong here
What is the value of x? Basically, use Long always instead of Integer.
@SJR Spot on! x is incrementing. I have edited my response.
I've declared both bottomL and x As Long, but still getting the same error
0

Try this

Option Explicit

Sub CopyRows()
    Dim bottomL As Long
    Dim x As Long
        bottomL = Sheets("Pacer").Range("L" & Rows.CountLarge).End(xlUp).Row: x = 1

    Dim c As Range
    For Each c In Sheets("Pacer").Range("A1:L" & bottomL)
        If (c.Value = "AFSB" Or c.Value = "TEIGIP4T" Or c.Value = "EPP") Then
            Intersect(c.Parent.Columns("A:Q"), c.EntireRow).Copy Worksheets("Portfolio").Range("A" & x + 1)
            x = x + 1
        End If
    Next c

End Sub

Reason explained here

4 Comments

I think we finally got, thanks for the refresh! Was the issue that I wasn't using CountLarge and/or the Explicit declaration at start? So frustrating it could come down to something that seemingly trivial
Because you were not using CountLarge you got a run time error. Explicit is not important here just best practise
Well thanks so much for the help, really appreciate it. This is something I use daily so important to get it right. Muchos gracias!
One more question, is it possible to augment your code slightly so it only copies values over as to preserve the source formatting in the destination tab?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.