Auto copy rows from one sheet to another based on sheet names and cell values

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
204
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim ws2 As Worksheet
Dim lastrow As Long
Dim lastrow2 As Long
Dim rownum As Long
Dim ws2name As String

Set ws = Sheets("RAW DATA")
lastrow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row

For rownum = 2 To lastrow
    Select Case ws.Cells(rownum, 9)
      Case "Fruit"
         ws2name = "Fruit"
         Case "Vegetable"
         ws2name = "Vegetable"
        Case "Nut"
         ws2name = "Nut"
      Case Else
        ws2name = ws.Cells(rownum, 5)
    End Select
    Set ws2 = Sheets(ws2name)
    lastrow2 = ws2.Cells(ws2.Rows.Count, "E").End(xlUp).Row
    ws.Rows(rownum).Copy ws2.Rows(lastrow2 + 1)
Next rownum

End Sub

I am using the above code to transfer data from RAW DATA sheets to different sheets based on the values of Column E and again if there is text "Fruit" or "Nut" or "Vegetable" then the data will be copied into those sheets, now I want that FOR EXAMPLE if there is text "Fruit" in Column I and in Column E against that row if text "APPLE" and if there is a sheet name " Fruit APPLE" then the data will be copied into Fruit sheet as well as Fruit APPLE sheet. Here I have used APPLE as an example in my data there are 100+ types of values in column E, so I want that if column H sheet name + column E sheet names matches then those data will be copied to those sheet name also. Kindly hep me with the code.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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