Copy rows to different sheets based on multiple criteria excel VBA macro

Jyotirmaya

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

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, 8)
      Case "A"
         ws2name = "A"

       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

election ed exp.xlsm
ABCDEFGH
1
21100151A111
32100452A222
43100473A333
54100524A444
651001035A555
761003086A666
871004017A777
98100558A888
1091004449A999
111010055510A101010
121110066611A111111
131210077712A121212
141310088813A131313
151410099914A141414
161510055515A151515
171610066616A161616
181710077717A171717
191810088818A181818
201910099919A191919
RAW DATA


I am using the above code to copy data from RAW DATA sheet to other sheets.
In column E, I have "A" is there.
but now I want that for example in case of text "A" in colum E, if Column B=100 & column C value range rows from 1 to 500 then it will copy those rows data to a new sheet A1 and if column B=100 and cell value range from 501-1000 then it will copy those rows data to a new sheet A2
I Dont want the data to copy in Sheet A, the data should be directly copied into A1 & A2 Sheets.
What should be the change in the code ?? please help.

In the above case I want first 10 rows to copy in A1 sheet and 11-20 rows into A2 sheet.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Do sheets A1 & A2 already exist, or do they need to be created?
 
Upvote 0
Is there any data on those two sheets? If so should that data be cleared, or the new data add below the existing data?
 
Upvote 0
Ok, how about
VBA Code:
Sub CopyToSheets()

Dim ws As Worksheet
Dim lastrow As Long
Dim rownum As Long

Set ws = Sheets("RAW DATA")
lastrow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
For rownum = 2 To lastrow
   If ws.Cells(rownum, 5) = "A" And ws.Cells(rownum, 2) = 100 Then
      Select Case ws.Cells(rownum, c)
         Case 1 To 500
            ws.Rows(rownum).Copy Sheets("A1").Cells(Rows.Count, 5).End(xlUp).Offset(1, -4)
         Case 501 To 1000
            ws.Rows(rownum).Copy Sheets("A2").Cells(Rows.Count, 5).End(xlUp).Offset(1, -4)
      End Select
   End If
Next rownum
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub CopyToSheets()

Dim ws As Worksheet
Dim lastrow As Long
Dim rownum As Long

Set ws = Sheets("RAW DATA")
lastrow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
For rownum = 2 To lastrow
   If ws.Cells(rownum, 5) = "A" And ws.Cells(rownum, 2) = 100 Then
      Select Case ws.Cells(rownum, c)
         Case 1 To 500
            ws.Rows(rownum).Copy Sheets("A1").Cells(Rows.Count, 5).End(xlUp).Offset(1, -4)
         Case 501 To 1000
            ws.Rows(rownum).Copy Sheets("A2").Cells(Rows.Count, 5).End(xlUp).Offset(1, -4)
      End Select
   End If
Next rownum
End Sub

Thank you for your help, but I got error message that - run time error 1004 application defined or object defined error
and when I clicked on debug it showed in yellow

VBA Code:
     Select Case ws.Cells(rownum, c)

what should be the chage ??
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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