Find cell containing a text in a Column from multiple sheets, then copy that row and paste it in a new sheet

tommiexboi

New Member
Joined
Apr 24, 2017
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a workbook with about 20 sheets.
Column A in each sheet contains "Purchase / Forecast - XXXXX", it will say "Purchase / Forecast" but the XXXXX can change.

I would like to create a VBA that will:

1) Go through each sheet and from Column A find the row that contains string "Purchase / Forecast"
2) Copy that Row
3) Paste that Row in a new sheet called "New" as a value (Paste in B1 is preferable)

Any help will be awesome.

Thanks!
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You said:
2) Copy that Row
3) Paste that Row in a new sheet called "New" as a value (Paste in B1 is preferable)

If we copy the row we must paste the entire row so we must start pasting in column A not column B

Try this:

VBA Code:
Sub Copy_Row_If()
'Modified  3/4/2021  12:01:56 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim b As Long
Dim c As Long
Dim s As Variant
Dim lastrow As Long
Dim lastrowa As Long
Dim ans As String
c = 1
Sheets.Add(Before:=Sheets(1)).Name = "New"

    For i = 2 To Sheets.Count
        ans = Sheets(i).Name
        lastrow = Sheets(i).Cells(Rows.Count, c).End(xlUp).Row
        lastrowa = Sheets("New").Cells(Rows.Count, 1).End(xlUp).Row + 1
   
        With Sheets(i).Cells(1, c).Resize(lastrow)
            .AutoFilter Field:=1, Criteria1:="=*Purchase / Forecast*", Operator:=xlFilterValues
            counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
            If counter > 1 Then
                .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("New").Cells(lastrowa, 1)
            Else
                MsgBox "No values found on " & vbNewLine & "Sheets " & Sheets(i).Name
            End If
            .AutoFilter
        End With
    Next

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Awesome! I really like the MSG box that's a great added touch.

Would it be possible to paste the row as values?
The rows have formula's in some of the cells and I would like to just bring over the values.
 
Upvote 0
Try this:
VBA Code:
Sub Copy_Row_If()
'Modified  3/4/2021  11:39:26 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim b As Long
Dim c As Long
Dim s As Variant
Dim lastrow As Long
Dim lastrowa As Long
Dim ans As String
c = 1
Sheets.Add(Before:=Sheets(1)).Name = "New"

    For i = 2 To Sheets.Count
        ans = Sheets(i).Name
        lastrow = Sheets(i).Cells(Rows.Count, c).End(xlUp).Row
        lastrowa = Sheets("New").Cells(Rows.Count, 1).End(xlUp).Row + 1
   
        With Sheets(i).Cells(1, c).Resize(lastrow)
            .AutoFilter Field:=1, Criteria1:="=*Purchase / Forecast*", Operator:=xlFilterValues
            counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
            If counter > 1 Then
                .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy: Sheets("New").Cells(lastrowa, 1).PasteSpecial xlPasteValues
            Else
                MsgBox "No values found on " & vbNewLine & "Sheets " & Sheets(i).Name
            End If
            .AutoFilter
        End With
    Next

Application.ScreenUpdating = True
End Sub
 
Upvote 0
This looks promising. I have an issue with a slight difference. I have a worksheet with a long list of customer names and an identifier along with some other information. I can easily sort them alphabetically, however, I need to separate all customer records (rows) for customers with names starting with 'A' in one worksheet, all those starting with 'B' in the second worksheet, and so on. So in brief, starting at row 2, the code needs to:

1) Copy all rows with customer name starting with A to the worksheet named 'A' (copy into rows 2 onwards)
2) repeat the same for every letter of the alphabet, copying the rows into separate sheets.

Can anyone please help?
 
Upvote 0
This looks promising. I have an issue with a slight difference. I have a worksheet with a long list of customer names and an identifier along with some other information. I can easily sort them alphabetically, however, I need to separate all customer records (rows) for customers with names starting with 'A' in one worksheet, all those starting with 'B' in the second worksheet, and so on. So in brief, starting at row 2, the code needs to:

1) Copy all rows with customer name starting with A to the worksheet named 'A' (copy into rows 2 onwards)
2) repeat the same for every letter of the alphabet, copying the rows into separate sheets.

Can anyone please help?
You need to start a new posting for this question.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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