Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Copy To Next Blank Line

Posted on 2011-09-13
4
317 Views
Last Modified: 2012-05-12
I get a runtime error 1004 “Select method of Range class failed” on this line.
Cells(NextRow, 1).Select

I have included a file.

Any help would be appreciated.


Public Sub CopyRows()
   Sheets("Sheet1").Select
    ' Find the last row of data
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    ' Loop through each row
    For x = 2 To FinalRow
        ' Decide if to copy based on column H
        ThisValue = Cells(x, 8).Value
        If ThisValue = "ir" Then
            Cells(x, 1).Resize(1, 8).Copy
            Sheets("a").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Sheet1").Select
        ElseIf ThisValue = "RR" Then
            Cells(x, 1).Resize(1, 33).Copy
            Sheets("b").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Sheet1").Select
        End If
    Next x

End Sub

Private Sub CommandButton1_Click()
CopyRows
End Sub

Open in new window

Copy-NextBlankRow.xls
0
Comment
Question by:ca1358
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 36532002
All that Selecting isn't making it clear what's active when.

Try this which basically does the same but doesn't use Select or Activate.

Option Explicit

Public Sub CopyRows()
Dim wsSrc As Worksheet
Dim wsA As Worksheet
Dim wsB As Worksheet
Dim NextRow As Long
Dim FinalRow As Long
Dim X As Long
Dim ThisValue

    Set wsSrc = Sheets("Sheet1")

    Set wsA = Sheets("A")
    Set wsB = Sheets("B")

    FinalRow = wsSrc.Cells(Rows.Count, 1).End(xlUp).Row

    ' Loop through each row
    For X = 2 To FinalRow
        ' Decide if to copy based on column H
        ThisValue = wsSrc.Cells(X, 8).Value

        If ThisValue = "ir" Then

            NextRow = wsA.Cells(Rows.Count, 1).End(xlUp).Row + 1

            wsSrc.Cells(X, 1).Resize(1, 8).Copy wsA.Cells(NextRow, 1)
        ElseIf ThisValue = "RR" Then
            NextRow = wsB.Cells(Rows.Count, 1).End(xlUp).Row + 1

            wsSrc.Cells(X, 1).Resize(1, 33).Copy wsB.Cells(NextRow, 1)


        End If
    Next X

End Sub

Private Sub CommandButton1_Click()
    CopyRows
End Sub

Open in new window

0
 
LVL 4

Accepted Solution

by:
grogman earned 500 total points
ID: 36532067
Or another really simple fix: replace  this line:

Cells(NextRow, 1).Select

with this line:

ActiveSheet.Cells(NextRow, 1).Select
0
 

Author Closing Comment

by:ca1358
ID: 36532134
Thank you
0
 
LVL 33

Expert Comment

by:Norie
ID: 36532195
Sorry if the code I posted was a bit too much, but you really should avoid using Select.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question