copy rows based on value

already

Board Regular
Joined
Nov 11, 2008
Messages
179
Hi

I'm using following code to copy rows to another sheet based on the value in column F. I can't combine the code to copy the different values to different sheets (ex all rows with value 'test1' go to sheet2 all with 'test2" go to sheet 3)without having empty rows. When I run different macros to do this I don't have any problem. The goal is to sort 1 sheet to 3 sheets (3 different values in column F)
Any help is welcome.

Thanks

Al
part of the code

If Range("F" & CStr(LSearchRow)).Value = "test" Then<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Sheets("Sheet2").Select<o:p></o:p>
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
<o:p></o:p>
'Move counter to next row<o:p></o:p>
LCopyToRow = LCopyToRow + 1<o:p></o:p>
<o:p></o:p>
Sheets("Sheet1").Select<o:p></o:p>
<o:p></o:p>
End If<o:p></o:p>
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This code worked for me:

Code:
Sub MoveRows()
Dim i As Integer
Dim Str1 As String, Str2 As String, Str3 As String
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Str1 = "First Search Term"  'Add your search terms
Str2 = "Second Search Term" 'Add your search terms
Str3 = "Third Search Term" 'Add your search terms
 
'Set Destination Worksheets
Set ws1 = Worksheets("Sheet2") 
Set ws2 = Worksheets("Sheet3")
Set ws3 = Worksheets("Sheet4")
 
For r = 1 To ActiveSheet.Range("F65536").End(xlUp).Row
    If Cells(r, 6).Value = Str1 Then
    i = ws1.Range("A65536").End(xlUp).Offset(1, 0).Row
    ws1.Rows(i).Value = ActiveSheet.Rows(r).Value
    End If
    If Cells(r, 6).Value = Str2 Then
    i = ws2.Range("A65536").End(xlUp).Offset(1, 0).Row
    ws2.Rows(i).Value = ActiveSheet.Rows(r).Value
    End If
    If Cells(r, 6).Value = Str3 Then
    i = ws3.Range("A65536").End(xlUp).Offset(1, 0).Row
    ws3.Rows(i).Value = ActiveSheet.Rows(r).Value
    End If
Next
End Sub
 
Upvote 0
Wow. This works for me in Excel 2010 as well. I had to search over 20 forums to find a simple, adaptable macro code. Thank you, Sthrncali!
 
Upvote 0
:confused: How might Sthrncali's code be modified to overwrite (rather than append) the data that already exists in the destination worksheets? If so, could someone kindly paste the final code excerpt?

:confused: Getting greedy, but could the code be modified further to always copy the first row of the active sheet, before performing the criteria-based copy/paste that Sthrncali's code already does?

Thank you for the help. I didn't know I needed VBA skills until a couple of days ago. This forum has been a big help to me, allowing me to move forward on my project even though my VBA learning has only started. :)
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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