VBA - Identify rows by value and copy and paste into new tab

JetSetDrive

New Member
Joined
Jul 26, 2019
Messages
14
I need assistance writing a code that will allow me to identify all rows that have a value of "Yellow" in column B. I then need to copy only the identified cells over to a new tab and label the new tab "Yellow"

I appreciate the help

Thank you.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:
Code:
Sub Filter_Me_Please()
'Modified  7/26/2019  9:12:59 PM  EDT
Application.ScreenUpdating = False
Dim lastrow As Long
Dim c As Long
Dim s As Variant
Dim ans As String
ans = ActiveSheet.Name
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Yellow"
c = 2 ' Column Number Modify this to your need
s = "Yellow" 'Search Value Modify to your need
lastrow = Sheets(ans).Cells(Rows.Count, c).End(xlUp).Row
With Sheets(ans).Cells(1, c).Resize(lastrow)
    .AutoFilter 1, s
    counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Yellow").Rows(1)
    Else
        MsgBox "No values found"
    End If
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Assuming your header is Row(1)
Then try this:
Code:
Sub Filter_Me_Please()
'Modified  7/26/2019  9:44:36 PM  EDT
Application.ScreenUpdating = False
Dim lastrow As Long
Dim c As Long
Dim s As Variant
Dim ans As String
ans = ActiveSheet.Name
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Yellow"
c = 2 ' Column Number Modify this to your need
s = "Yellow" 'Search Value Modify to your need
lastrow = Sheets(ans).Cells(Rows.Count, c).End(xlUp).Row
With Sheets(ans).Cells(1, c).Resize(lastrow)
    .AutoFilter 1, s
    counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Yellow").Rows(2)
        Sheets(ans).Rows(1).Copy Sheets("Yellow").Rows(1)
    Else
        MsgBox "No values found"
    End If
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here is another approach that you could try in a copy of your workbook.
Code:
Sub Copy_Rows()
  Dim wsOld As Worksheet, wsNew As Worksheet
  
  Set wsOld = ActiveSheet
  Sheets.Add(After:=wsOld).Name = "Yellow"
  Set wsNew = Sheets(wsOld.Index + 1)
  With wsNew
    .Range("A1:A2").Value = Application.Transpose(Array(wsOld.Range("B1").Value, "Yellow"))
    wsOld.UsedRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("A1:A2"), CopyToRange:=.Range("B1"), Unique:=False
    .Columns(1).Delete
  End With
End Sub
 
Upvote 0
Yes there are always 20 different ways to do everthing with Excel.
Here is another approach that you could try in a copy of your workbook.
Code:
Sub Copy_Rows()
  Dim wsOld As Worksheet, wsNew As Worksheet
  
  Set wsOld = ActiveSheet
  Sheets.Add(After:=wsOld).Name = "Yellow"
  Set wsNew = Sheets(wsOld.Index + 1)
  With wsNew
    .Range("A1:A2").Value = Application.Transpose(Array(wsOld.Range("B1").Value, "Yellow"))
    wsOld.UsedRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("A1:A2"), CopyToRange:=.Range("B1"), Unique:=False
    .Columns(1).Delete
  End With
End Sub
 
Upvote 0
Yes there are always 20 different ways to do everthing with Excel.
Any reason the OP should only get offered one of them?

BTW, our codes do not do exactly the same thing. Yours may well do what the OP wants and mine might not. It could be the other way around, or they might both do what the OP wants - depends on the detail of the data and the requirement.
Further reason to offer choice.
 
Upvote 0
Just another way.

Code:
Sub Copy_Yellow()
  Dim sh As Worksheet
  Set sh = ActiveSheet
  sh.Range("A1").CurrentRegion.AutoFilter 2, "Yellow"
  Sheets.Add(, Sheets(Sheets.Count)).Name = "Yellow"
  sh.AutoFilter.Range.EntireRow.Copy Range("A1")
  sh.ShowAllData
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,786
Messages
6,126,893
Members
449,347
Latest member
Macro_learner

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