Copy and paste rows that contain certain text to a seperate tab VBA beginner

VBAlearner1

New Member
Joined
Apr 11, 2013
Messages
4
Hi

I'd appreciate some help. I know excel quite well but I'm new to VBA.

I'd like a code that copy and pastes complete rows from sheet 1 to sheet 2, however I only want to copy and paste the rows that have certain text (the number 1) in column G.

Can anyone help please?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Welcome to MrExcel.

AutoFilter your data for column G equals 1 and copy/paste the visible cells. The macro recorder should give you some VBA code that you can adapt. Hint: use SpecialCells(xlCellTypeVisible).
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
With Sheets("Sheet1")
    LR = .Range("G" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        If .Range("G" & i).Value = 1 Then .Rows(i).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next i
End Sub
 
Upvote 0
Another approach ... put the following code into the code module for your worksheet (not into a regular module).\
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("G:G")) Is Nothing Then Exit Sub
    If Target = "1" Then
        Target.EntireRow.Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    End If
End Sub
 
Upvote 0
Thank you to all...

Vog am i putting this code into the sheet code? How does this code run? Sorry I'm new to VBA.

umps could you explain how I get to where your describing please?
 
Upvote 0
Press ALT + F11 to open the Visual Basic Editor, select Module from the Insert menu then paste in the code. Press ALT + Q.

Press ALT + F8 then double click test
 
Upvote 0
Right click the tab that contains your data and then click 'View Code'. Paste the code into the winidow that appears and then close the window. Each time you enter 1 in column G. the row will be copied to Sheet2.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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