VBA - Pulling a row from each sheet based on column B value, pasting in new sheet

Greshapa

New Member
Joined
May 23, 2022
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hello,

Its my first time posting here, thanks in advance for any assistance :)

I have a workbook with about 50 sheets, each sheet is a monthly report (seen blow) with production and sales data for various products. What I would like to do, is extract one products data from each sheet (Lets say, the "2A" products row that i circled in red) and bring it into its own new sheet. In this new 2A sheet, there will be a table just like in my screenshot, except the only product will be 2A, and each row will be one month of 2A data

I'm new to using VBA so my issues are that 1.) the products data is not always in the same row (But it is always in column B) and 2.) I'm not sure how to make a macro that would switch back and forth between various sheets and copy data to a new sheet. All of my previous VBA macro experience has been taking data from one sheet, copying it to a new sheet, and formatting it to look nice.
 

Attachments

  • Capture.PNG
    Capture.PNG
    36.1 KB · Views: 24

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello, give this a try:
VBA Code:
Sub Product_Consolidate()
Dim NewWs As Worksheet
Dim i As Long
Dim ws As Worksheet
Dim lrow As Long
Dim NewWsLrow
Dim Product As String

Product = "2B" '***Specify product name here

Set NewWs = Sheets.Add(After:=Sheets(Sheets.Count))  'Create new sheet and name it the product name
NewWs.Name = Product

Sheets(1).Range("B3:H3").Copy NewWs.Range("B3:H3") 'Paste headers to new sheet
NewWs.Range("I3") = "SheetName"

For Each ws In ActiveWorkbook.Worksheets 'This line starts the loop through each sheet
    If ws.Name <> NewWs.Name Then 'May need to add criteria to only run this on sheets that are a month name (I'm unsure what your workbook sheets look like)
        lrow = ws.Cells(Rows.Count, 2).End(xlUp).Row
        For i = 4 To lrow '4 represents the row the data starts in. This line starts the loop to look through the product in each sheet
            If ws.Cells(i, 2) = Product Then
                NewWsLrow = NewWs.Cells(Rows.Count, 2).End(xlUp).Row
                ws.Cells(i, 2).EntireRow.Copy NewWs.Cells(NewWsLrow + 1, 2).EntireRow 'If the loop finds the product name, then paste it to the new sheet
                NewWs.Cells(NewWsLrow + 1, 9) = ws.Name 'Add in the name of the sheet the data is coming from
            End If
        Next i
    End If
Next ws

End Sub

It will loop through all sheets, and then loop through all rows on the sheet to look for the product "2A" (2A can be edited in the code where I commented at the top). Once it finds the product it will paste the entire row to a new sheet with titled the name of the product. I also added a column in the new sheet that has the name of the original sheet the data came from.

The call-outs to this code is that :
1. You have to hard-code the sheet into the code (there's ways around this with input boxes that I can add for you if you'd like)
2. That you can only run it once per product, as running it a second time will error because a sheet with the product name already exists.
 
Upvote 0
Solution
lets says u extraced all to Data sheets

Sub test()
Dim i, x, ws As Worksheet
Dim lastrow As Long, destrow As Long
Dim product

product = Application.InputBox("Enter Product")
destrow = Sheets("Data").Cells(Rows.Count, "A").End(xlUp).Row
For Each ws In Worksheets
If ws.Name <> "Data" Then
With ws
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row

For x = 1 To lastrow
If .Cells(x, "B").Value = product Then
.Rows(x).Copy Destination:=Sheets("Data").Rows(destrow)
destrow = destrow + 1
End If
Next x
End With
End If
Next ws
End Sub
please consider
this will copy all rows that contain entered product name (column B) in input box to last row column A in Sheets Data
 
Last edited:
Upvote 0
Thanks so much, both of you!!! Max, that worked perfectly with a few small tweaks. The main thing i had to change was `For i = 4 To lrow` to `For i = 4 To 25` so that I didnt pick up un needed rows that weren't visible in my screenshot


I really appreciate it!
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,792
Members
449,126
Latest member
Greeshma Ravi

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