Copy filtered items to new sheet

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
301
Office Version
  1. 365
Platform
  1. Windows
Hi,
I need a macro to filter and copy items to a new sheet. The filtered items and the new sheet name to be established in the macro.

Book2
ABCDEF
1ABCDEF
2june3-31
3june31111
4april3111
5may31111
6february311
7october311
8october311
9november311
10december311
11
12
13result
14ABCDEF
15june3-31
16june31111
17may31111
18october311
19october311
20sheet name"months"
sheet1
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi @doriannjeshi. Very interesting but nothing clear. By what criteria are you going to filter the range? There is no logic in your example. Please explain in more detail (I am not taking on the task).
 
Upvote 0
Hi,
I need a macro to filter and copy items to a new sheet. The filtered items and the new sheet name to be established in the macro.

Book2
ABCDEF
1ABCDEF
2june3-31
3june31111
4april3111
5may31111
6february311
7october311
8october311
9november311
10december311
11
12
13result
14ABCDEF
15june3-31
16june31111
17may31111
18october311
19october311
20sheet name"months"
sheet1

Try this.

This is similar to your recent post.

The required changes are indicated.

VBA Code:
Public Sub subCopyFilteredItemsToNewSheet()
Dim Ws As Worksheet
  
  ActiveWorkbook.Save
  
  Set Ws = Sheets("Sheet1") ' <- Change to the sheet where the data is.
  
  Application.DisplayAlerts = False
  On Error Resume Next
  Sheets("NewSheet").Delete ' <- Change to the new sheet name.
  On Error GoTo 0
  Application.DisplayAlerts = True
  
  With Ws
  
    .Range("A1").AutoFilter 2, 3 ' <- Change to the column number from the 2 and to the value from the 3.
  
    Sheets.Add(, Sheets(Sheets.Count)).Name = "NewSheet" ' <- Change to the new sheet name.
    
    .AutoFilter.Range.EntireRow.Copy Range("A1")
  
    .AutoFilterMode = False
  
  End With
   
End Sub
 
Upvote 0
Sorry I wasn't clear
The months are to be filtered and copied and to be specified which ones in the macro
 
Upvote 0
A correct statement of the problem implies a correct and optimal solution. But your description is not like that.
 
Upvote 0
Sorry I wasn't clear
The months are to be filtered and copied and to be specified which ones in the macro

Note where the changes are required.

VBA Code:
Public Sub subCopyFilteredItemsToNewSheet()
Dim Ws As Worksheet
Dim strMonths As String
Dim strNewSheet As String

  ActiveWorkbook.Save
  
  Set Ws = Sheets("Sheet1") ' <- Change to the sheet where the data is.
  
  strMonths = "January,February,March,December" ' <- Change to the required months.
  
  strNewSheet = "NewSheet" ' <- Change to the new sheet name.
  
  Application.DisplayAlerts = False
  On Error Resume Next
  Sheets(strNewSheet).Delete
  On Error GoTo 0
  Application.DisplayAlerts = True
  
  With Ws
  
    Sheets.Add(, Sheets(Sheets.Count)).Name = strNewSheet
    
    .Range("A1").AutoFilter Field:=1, Criteria1:=Split(strMonths, ","), Operator:=xlFilterValues
    
    .AutoFilter.Range.EntireRow.Copy Range("A1")
  
    .AutoFilterMode = False
    
    Cells.EntireColumn.AutoFit
  
  End With
   
End Sub
 
Upvote 0
Solution
Thank you all ,
@HighAndWilder excellent

I pressed your post as a solution , it accepts it but it used to show after. Now it doesn't show the solution on my end. I don't know if is a change or a bug
 
Upvote 0
I pressed your post as a solution , it accepts it but it used to show after. Now it doesn't show the solution on my end. I don't know if is a change or a bug
It is possible that you accidentally clicked it a second time and the tick was removed. Have you tried to mark the post again?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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