在AutoFilter上运行宏并在新工作表中显示数据

时间:2013-04-03 18:22:36

标签: excel excel-vba autofilter vba

其实我想做什么,我有自动过滤的数据,

enter image description here

- >我想为从过滤中选择的每个唯一名称创建新工作表。如果选择了John和Alex,则应为John创建一个新表,为Alex创建第二个表,并且每个表显示自己的数据(Name + No + R)。当下一次如果主表更新时,我应该在运行宏时附加新闻数据。我正在使用以下代码,但它不能100%工作。

Sub mycar()
   x = 2
   Do While Cells(x, 1) <> ""
   If Cells(x, 1) = "John" Then
   Worksheets("Sheet1").Rows(x).Copy
   Worksheets("Sheet2").Activate
   eRow = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
   ActiveSheet.Paste Destination:=Worksheets("Sheet2").Rows(eRow)
   End If
   Worksheets("Sheet1").Activate
   x = x + 1
   Loop
End Sub

- &GT;这里只复制写在引号中的单个数据。

- &GT;第二次,如果我运行此代码,它会再次使用新数据附加相同的数据。

帮助我避免这种错误。

谢谢。

4 个答案:

答案 0 :(得分:2)

如上所述,还有其他方法可以在过程中在Array中设置过滤器参数。代码看起来像这样:

Sub Solution()

Dim shData As Worksheet
    Set shData = Sheets("Arkusz1")    'or other reference to data sheet
Dim shNew As Worksheet
    shData.Activate
'get unique values based on Excel features
Range("a1").AutoFilter

Dim myArr As Variant
    myArr = Array("John", "max")

Range("a1").AutoFilter

Dim i As Long
For i = 0 To UBound(myArr)
    shData.Range("$A$1").AutoFilter Field:=1, Criteria1:=myArr(i), _
        Operator:=xlAnd
On Error Resume Next
    Sheets(myArr(i)).Range("A1").CurrentRegion.ClearContents

If Err.Number = 0 Then
    Range("A1").CurrentRegion.Copy Sheets(myArr(i)).Range("A1")
Else
    Set shNew = Sheets.Add(After:=Sheets(Sheets.Count))
    shData.Range("A1").CurrentRegion.Copy shNew.Range("A1")
    shNew.Name = myArr(i)
    Err.Clear
End If

Next i
'removing filter in master sheet
shData.Range("a1").AutoFilter

End Sub

答案 1 :(得分:0)

Worksheets("Sheet1").Rows(x).Copy

替换Worksheets("Sheet1").Rows(x).EntireRow.Copy

在添加信息之前清除目标工作表。

答案 2 :(得分:0)

我经常做类似的练习。因此,我在代码中提供了一些完整的解决方案和一些注释。它适用于A列中的所有唯一值,并创建(如果不存在)表单,其中相应的名称等于过滤器参数。

Sub Solution()
Dim shData As Worksheet
    Set shData = Sheets("Arkusz1")    'or other reference to data sheet
Dim shNew As Worksheet

'get unique values based on Excel features
'i guess some will not like it but I do :)
Range("a1").AutoFilter
Range("A1").CurrentRegion.Columns(1).Copy Range("ww1")
Range("ww1").CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlYes
'be sure that range where you copy (like ww1) is empty range around 

Dim myArr As Variant
    myArr = Range(Range("ww2"), Range("ww2").End(xlDown))
Range("ww1").CurrentRegion.ClearContents     'some cleaning
Range("a1").AutoFilter '

Dim i As Long
For i = 1 To UBound(myArr, 1)
    ActiveSheet.Range("$A$1").AutoFilter Field:=1, Criteria1:=myArr(i, 1), _
        Operator:=xlAnd
On Error Resume Next
    'this is for two reason- to check if appropriate sheet exists, if so to clean top area
    'if you need to append you would comment this line
    Sheets(myArr(i, 1)).Range("A1").CurrentRegion.ClearContents

If Err.Number = 0 Then
    'if you need to append only you would need to set range-to-copy a bit different
    Range("A1").CurrentRegion.Copy Sheets(myArr(i, 1)).Range("A1")
Else
    Set shNew = Sheets.Add(After:=Sheets(Sheets.Count))
    shData.Range("A1").CurrentRegion.Copy shNew.Range("A1")
    shNew.Name = myArr(i, 1)
    Err.Clear
End If

Next i

End Sub

这不能完全满足您的要求,但可能是相应改进的完整解决方案。

答案 3 :(得分:0)

标题##下面的代码符合您的要求。根据您的要求进行修改。


Private Sub Worksheet_Calculate()
  Dim x As Integer
  Dim rnge As Integer
  x = Range(Selection, Selection.End(xlDown)).Count
  rnge = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count

  If Range("E1").Value > rnge Then
   Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Copy
   Sheets(2).Select
   ActiveSheet.Paste
  End If
End Sub