View Full Version : [SOLVED:] How to use an array to copy filter range into new worksheet?
idnoidno
05-20-2017, 05:22 PM
19240I want to filter "January" and copy the corresponding value(B column) to the new worksheet,Any help will be appreciated19240
Option Explicit
Sub test()
    
    Rows(1).Insert
    With Range("A1").CurrentRegion
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="january"
        If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
            Worksheets.Add
            .Columns(2).Offset(1).Copy Range("A1")
        End If
        .AutoFilter
        .Rows(1).Delete
    End With
    
End Sub
idnoidno
05-20-2017, 08:23 PM
Result: 
   A  B
1  1
2  1
3  1
Below  is  I want .
A
B
january
1
january
1
january
1
> .Columns(2).Offset(1).Copy Range("A1") 
   .Offset(1).Copy Range("A1")
mdmackillop
05-21-2017, 05:10 AM
Please don't quote whole posts, only those lined relevant to your question.
mdmackillop
05-21-2017, 05:24 AM
Use the Macro recorder
'Recorded macro
Sub Macro14()
 Rows("1:1").Select
    Selection.Insert Shift:=xlDown
    Columns("A:B").Select
    Selection.AutoFilter
    Range("E15").Select
    Selection.AutoFilter
    Columns("A:A").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$31").AutoFilter Field:=1, Criteria1:="january"
    Columns("A:B").Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Sheets("???1").Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
End Sub
A little bit of editing
'Edited macro
Sub Macro15()
    Dim Sh
    Set Sh = ActiveSheet
    With Sh
    .Rows("1:1").Insert
    .Columns("A:A").AutoFilter Field:=1, Criteria1:="january"
    .Columns("A:B").Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Application.CutCopyMode = False
    .Rows("1:1").Delete
    End With
End Sub
Sub M_snb()
  Sheets.Add , Sheets(Sheets.Count)
  With Sheets(1).Cells(1).CurrentRegion
    .AutoFilter 1, "january"
    .Copy Sheets(Sheets.Count).Cells(1)
    .AutoFilter
  End With
End Sub
idnoidno
05-21-2017, 06:57 AM
.Offset(1).Copy Range("A1")
It is done.
But it is a little diffcult for me to understand.Would you plz to explain it?
idnoidno
05-21-2017, 07:11 AM
This is the simplest CODE that gets the same result.
You can use "F8 key" to execute the code line by line.
idnoidno
05-21-2017, 07:45 AM
Sheets.Add , Sheets(Sheets.Count)
    With Sheets(1).Cells(1).CurrentRegion
        .AutoFilter 1, "january"
        .Copy Sheets(Sheets.Count).Cells(1)
        .AutoFilter
    End With
End Sub
19244
If you get from the worksheet 1 worksheet 2, how to edit the original CODE ?
mancubus
05-23-2017, 04:18 AM
using sheet name
Sub M_snb()
    With Sheets("Sheet1").Cells(1).CurrentRegion 
        .AutoFilter 1, "january" 
        .Copy Sheets("Sheet2").Cells(1) 
        .AutoFilter 
    End With 
End Sub 
using sheet index
Sub M_snb()
    With Sheets(1).Cells(1).CurrentRegion 
        .AutoFilter 1, "january" 
        .Copy Sheets(2).Cells(1) 
        .AutoFilter 
    End With 
End Sub
mdmackillop
05-23-2017, 04:43 AM
Hi Idnoidno
When you copy code, use "Paste without formatting" (Ctrl + Shift +V) to avoid the Font tags etc.
mancubus
05-23-2017, 06:12 AM
modified:
Sub M_snb()
    With Sheets("Sheet1").Cells(1).CurrentRegion 
        .AutoFilter 1, "january" 
        .Copy
        Sheets("Sheet2").Cells(1).PasteSpecial xlPasteValues
        .AutoFilter 
    End With 
End Sub 
using sheet index
Sub M_snb()
    With Sheets(1).Cells(1).CurrentRegion 
        .AutoFilter 1, "january" 
        .Copy
        Sheets(2).Cells(1).PasteSpecial xlPasteValues
        .AutoFilter 
    End With 
End Sub 
[/QUOTE]
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.