VBA Rows.Copy Problem

beartooth91

Board Regular
Joined
Dec 15, 2024
Messages
64
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Not sure what I'm doing wrong here, its only pasting the last entry. There should be 6 lines pasted.

VBA Code:
Sub ForEa_Test()
'
With Worksheets("Combined")
  Dim Lrw As Long
  Lrw = .Cells(Rows.Count, "B").End(xlUp).Row

  For Each cell In .Range("B8:N" & Lrw)
    If cell.Value = "DI" Then
       cell.EntireRow.Copy Destination:=Sheets("New2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    End If
  Next cell
End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Not certain what is happening there ... but ... your macro works here.

???
 
Upvote 0
Not certain what is happening there ... but ... your macro works here.

???
That's strange. If I change the .Copy piece to Interior.ColorIndex = 6; it highlights all 6 entries on the source worksheet. When I put .Copy/Destination back in; it only pastes the last row entry.
 
Upvote 0
I don't know what to suggest. Sorry.
 
Upvote 0
Is there anything in col A?
 
Upvote 0
Screenshot 2025-01-01 111651.jpg
Is there anything in col A?
Screenshot of the worksheet I'm copying from.
Changed the code to the below and it does exactly the same thing as the previous version....

VBA Code:
Sub ForEa_Test()
'
With Worksheets("Combined")
  Dim Lrw As Long, i As Long, cell As Variant
  Lrw = .Cells(Rows.Count, "B").End(xlUp).Row

  '
  For i = 8 To Lrw
    If .Cells(i, "E").Value = "DI" Then
       .Cells(i, "E").EntireRow.Interior.ColorIndex = 34
       .Cells(i, "E").EntireRow.Copy Destination:=Sheets("New2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    End If
  Next i
End With
End Sub
 
Upvote 0
Hi *beartooth91

try this modified code:
VBA Code:
Sub ForEa_Test()
'
    Dim cell As Range                                                                                     ' <<< added
    Dim nl As Long                                                                                        ' <<< added
    
    With Worksheets("Combined")
        Dim Lrw As Long
        Lrw = .Cells(Rows.Count, "B").End(xlUp).Row
        
        For Each cell In .Range("B8:N" & Lrw)
            If cell.Value = "DI" Then
                nl = Sheets("New2").Cells(Rows.Count, 2).End(xlUp).Row + 1                                     ' <<< added
'                cell.EntireRow.Copy Destination:=Sheets("New2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)   ' <<< deleted
                cell.EntireRow.Copy Destination:=Sheets("New2").Rows(nl)                                       ' <<< added
            End If
        Next cell
    End With
End Sub

and let us know

Bye
 
Upvote 0
Solution
There is nothing in col A so this line
VBA Code:
Sheets("New2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
will always copy the data to row 2. You need to change it to look at col B (ie 2) not col A.
VBA Code:
Sheets("New2").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
 
Upvote 0
Hi *beartooth91

try this modified code:
VBA Code:
Sub ForEa_Test()
'
    Dim cell As Range                                                                                     ' <<< added
    Dim nl As Long                                                                                        ' <<< added
   
    With Worksheets("Combined")
        Dim Lrw As Long
        Lrw = .Cells(Rows.Count, "B").End(xlUp).Row
       
        For Each cell In .Range("B8:N" & Lrw)
            If cell.Value = "DI" Then
                nl = Sheets("New2").Cells(Rows.Count, 2).End(xlUp).Row + 1                                     ' <<< added
'                cell.EntireRow.Copy Destination:=Sheets("New2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)   ' <<< deleted
                cell.EntireRow.Copy Destination:=Sheets("New2").Rows(nl)                                       ' <<< added
            End If
        Next cell
    End With
End Sub

and let us know

Bye
This worked. Thanks. The only problem is I'm not understanding why my version didn't work. Your solution agrees with Fluff's post......I'm just not really understanding what Column A has to do with it......?
 
Upvote 0
in this snippet of code

VBA Code:
nl = Sheets("New2").Cells(Rows.Count, 2).End(xlUp).Row + 1                                     ' <<< added

with Cells(Rows.Count, 2) I read which is the first free cell of column B (2) because in your example A (1) is empty.

Then with "Row" I apply that row number to the copy
 
Upvote 0

Forum statistics

Threads
1,225,623
Messages
6,186,065
Members
453,336
Latest member
Excelnoob223

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top