我有一个包含2列的工作表(主要),如下所示:
姓名国籍
约翰史密斯英国人
Chris Banks American
Jean Pierre French
我的要求是根据国籍数据将Worksheet(main)中的单元格数据传输到辅助工作表。英国的辅助工作表应如下所示:
姓名国籍
约翰史密斯英国人
只有与英国国籍相关的数据才能按照上述过滤到此工作表。我已经看过使用粘贴链接,但这并不完全符合我的要求。非常感谢任何帮助/建议。提前谢谢!
答案 0 :(得分:0)
首先创建一个工作簿,然后在“Sheet1”中输入您需要的数据:
Sheet1 http://im59.gulfup.com/fULkNN.png
然后添加如上所示的命令按钮(ActiveX)并添加代码。
所以我为你写的代码将是:
代码:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
For Each Worksheet In ActiveWorkbook.Worksheets
Worksheet.Visible = True
Next
For Each Worksheet In ActiveWorkbook.Worksheets
If Worksheet.Name <> "Sheet1" Then Worksheet.Delete
Next
Worksheets.Add().Name = "TempSheet"
lastrow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).Row
Dim copyRange As Range
Set copyRange = Worksheets("Sheet1").Range("B2:B" & lastrow)
copyRange.Copy Destination:=Worksheets("TempSheet").Range("A1")
On Error Resume Next
Lastrow2 = Worksheets("TempSheet").Cells(Worksheets("TempSheet").Rows.Count, "A").End(xlUp).Row
Worksheets("TempSheet").Range("$A$1:$A$" & Lastrow2).RemoveDuplicates Columns:=1, Header:=xlNo
Lastrow3 = Worksheets("TempSheet").Cells(Worksheets("TempSheet").Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow3
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = Worksheets("TempSheet").Range("A" & i).Value
With Worksheets("Sheet1")
.AutoFilterMode = False
With .Range("A1:B" & lastrow)
.AutoFilter
.AutoFilter Field:=2, Criteria1:=Worksheets("TempSheet").Range("A" & i).Value, Operator:=xlFilterValues
Set copyFrom = .SpecialCells(xlCellTypeVisible).EntireRow
End With
End With
With Worksheets(Worksheets("TempSheet").Range("A" & i).Value)
copyFrom.Copy .Rows(1)
End With
Worksheets(Worksheets("TempSheet").Range("A" & i).Value).Columns("A:Z").AutoFit
With Worksheets("Sheet1")
.AutoFilterMode = False
End With
Next i
Worksheets("TempSheet").Delete
Sheets("Sheet1").Activate
Application.DisplayAlerts = True
End Sub
我相信这可以满足你的需要。