Macro/ VBA - Move rows to new worksheet

Joined
Nov 5, 2010
Messages
8
Hi there, I was wondering if anyone out there might be able to help me, I am looking for a way to take all rows from a spreadsheet where the value in column U is 'closed' and transfer them automatically to a different worksheet. I discovered the world of Macro and VBA today and have been quite enjoying it actually, but unfortunately despite my best attempts at adapting similar macros, I have not yet been able to produce any results. Could anyone help me or provide any advice with this? Or perhaps advise on how I can adapt a similar macro?

Many thanks in advance techsperts ;)
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the board.

Assuming your first row is your header row (starting in cell[A1]) and the data is in the first spreadsheet and by transfer you mean cut and paste try:
Code:
Sub DeleteClosedInColumnU ()
With Sheets("Sheet1")
    If .AutoFilterMode Then Cells.AutoFilter
    .Range("A1").CurrentRegion.AutoFilter field:=21, Criteria1:="Closed"
    .Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).EntireRow.Cut Sheets("Sheet2").Range("A1") 
    .Range("A1").CurrentRegion.AutoFilter
End With
End Sub
Warning not tested, try on some dummy data
 
Upvote 0
Hey JackDanIce, thanks for the response, just gave it a go and I got the message-

"Run time error 9

Subscript out of range"

Any ideas?

Thanks again for responding
 
Upvote 0
Don't know if this is helpful, but this is another VBA script that was suggested (and apparently worked) for another user on the forum. I did try altering it myself but it seems my knowledge does not yet extend far enough for this! Here's the code-

Sub Extract_Data()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
'this macro assumes that your first row of data is a header row.<o:p></o:p>
'will copy a row from one worksheet, to another blank workbook<o:p></o:p>
'IF there is a “closed” in column U<o:p></o:p>
'Variables used by the macro<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
Dim FilterCriteria<o:p></o:p>
Dim CurrentFileName As String<o:p></o:p>
Dim NewFileName As String<o:p></o:p>
<o:p> </o:p>
Set a = ActiveSheet<o:p></o:p>
'<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:eek:ffice:smarttags" /><st1:place w:st="on"><st1:PlaceName w:st="on">Select</st1:PlaceName> <st1:PlaceType w:st="on">Range</st1:PlaceType></st1:place><o:p></o:p>
'(note you can change this to meet your requirements)<o:p></o:p>
Range("A1:AS4000").Select<o:p></o:p>
'Apply Autofilter<o:p></o:p>
Selection.AutoFilter<o:p></o:p>
FilterCriteria = closed<o:p></o:p>
'NOTE - this filter is on column U (field:=21), to change<o:p></o:p>
'to a different column you need to change the field number<o:p></o:p>
Selection.AutoFilter field:=21, Criteria1:=FilterCriteria<o:p></o:p>
'Select the visible cells (the filtered data)<o:p></o:p>
Selection.SpecialCells(xlCellTypeVisible).Select<o:p></o:p>
'Copy the cells<o:p></o:p>
Selection.Copy<o:p></o:p>
'Open a new file<o:p></o:p>
'Workbooks.Add Template:="Workbook"<o:p></o:p>
'Get this file's name<o:p></o:p>
'NewFileName = ActiveWorkbook.Name<o:p></o:p>
Sheets.Add().Name = "closed"<o:p></o:p>
Set b = ActiveSheet<o:p></o:p>
'Make sure you are in cell A1<o:p></o:p>
Range("A1").Select<o:p></o:p>
'Paste the copied cells<o:p></o:p>
ActiveSheet.Paste<o:p></o:p>
Range("A1").Select 'unselect everything<o:p></o:p>
'Clear the clipboard contents<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
'Go back to the original file<o:p></o:p>
'Workbooks(CurrentFileName).Activate<o:p></o:p>
a.Select<o:p></o:p>
Selection.AutoFilter field:=21, Criteria1:=FilterCriteria<o:p></o:p>
Selection.SpecialCells(xlCellTypeVisible).Select<o:p></o:p>
'Clear the autofilter<o:p></o:p>
'Selection.AutoFilter field:=1<o:p></o:p>
'Take the Autofilter off<o:p></o:p>
Selection.AutoFilter<o:p></o:p>
'Go to A1<o:p></o:p>
'Range("A1").Select<o:p></o:p>
Application.ScreenUpdating = True<o:p></o:p>
End<o:p></o:p>
End Sub


:)
 
Upvote 0
What is the worksheet your data is in and what is the entire range your data is in?
 
Upvote 0
Hi ok it's a worksheet with a list of suggested improvements. In the u column there is formatting to mark each cell as either open, overdue, or closed. I've been asked to set something up so that when a 'case' has been closed, the whole row automatically jumps to a second worksheet- so there ends up being a new worksheet for closed cases. The current range of the sheet is a-y and 1-150. Is this what you meant? Hope I haven't misunderstood.
 
Upvote 0
Ok this code will move your data from the first sheet to the second sheet in the same workbook - I'm having to guess where the source data is and where you want it moved to:
Code:
Sub DeleteClosedInColumnU ()
Dim i as Long
i = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
With Sheets(1)
    If .AutoFilterMode Then Cells.AutoFilter
    .Range("A1:Y" & i).AutoFilter field:=21, Criteria1:="Closed"
    .Range("A1:Y" & i).SpecialCells(xlCellTypeVisible).EntireRow.Cut Sheets(2).Range("A1") 
    .Range("A1").CurrentRegion.AutoFilter
End With
End Sub
 
Upvote 0
Hi ok I just ran this and got another message-


Run time error '1004'
The command could not be completed using the range specified. Select a single cell within the range and try the command again.


Hmm. Pressing debug the 6th line has been highlighted. What information do you need about the source data and target location?
 
Upvote 0
Oops please excuse me I was trying it on an old version of the file to be safe, and the columns are in a different order. Will run again on an updated version. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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