VBA to Copy and Paste Rows if Condition

plastakis

New Member
Joined
Apr 10, 2019
Messages
23
Hi, made mistake in VBA code. I need that copy all rows from one sheet to another, but copy just last row. Please, advice me where I made mistake.

Sub LastRowInOneColumn()
Dim LastRow As Long
Dim i As Long, j As Long

'Find the last used row in a Column: column A in this example
With Worksheets("Suskirstymas")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

MsgBox (LastRow)
'first row number where you need to paste values in Sheet1'
With Worksheets("Pasiulymas")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With

For i = 1 To LastRow
With Worksheets("Suskirstymas")
If .Cells(i, 1).Value = "T" Then
.Rows(i).Copy
Worksheets("Pasiulymas").Range("A" & j).PasteSpecial Paste:=xlPasteValues
End If
End With
Next i
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try
Code:
For i = 1 To Lastrow
   With Worksheets("Suskirstymas")
      If .Cells(i, 1).Value = "T" Then
         .Rows(i).Copy
         Worksheets("Pasiulymas").Range("A" & j).PasteSpecial paste:=xlPasteValues
        [COLOR=#ff0000] j = j + 1[/COLOR]
      End If
   End With
Next i
Otherwise you are constantly overwriting the row with new info.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,409
Messages
6,119,339
Members
448,888
Latest member
Arle8907

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