码迷,mamicode.com
首页 > 其他好文 > 详细

利用excel拆分数据

时间:2016-09-25 20:29:56      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

要求:将sheet1中的数据按照公司名称拆分到不同的工作表

使用VBA:

1:打开sheet1的查看代码

2:运行

······························································································

Sub cfs()

Dim GSArr() As String ‘公司名称清单

Dim Rca As Long ‘A列数据行数

Dim i As Integer

Dim Sn As String

 

Sn = ActiveSheet.Name

Rca = Columns("A:A").End(xlDown).Row

 

ReDim GSArr(1 To 1)

GSArr(1) = Cells(2, 1)

For i = 3 To Rca

    If IsError(Application.Match(Cells(i, 1), GSArr, 0)) Then

       ReDim Preserve GSArr(1 To UBound(GSArr) + 1)

       GSArr(UBound(GSArr)) = Cells(i, 1)

    End If

Next

 

If ActiveSheet.AutoFilterMode = False Then

    Rows("1:1").AutoFilter

Else

    If ActiveSheet.FilterMode = True Then ActiveSheet.ShowAllData

End If

 

For i = 1 To UBound(GSArr)

    ActiveSheet.Cells.AutoFilter Field:=1, Criteria1:=GSArr(i)

    Sheets.Add After:=Sheets(Sheets.Count)

    ActiveSheet.Name = GSArr(i)

    Sheets(Sn).Cells.Copy ActiveSheet.Cells

    Sheets(Sn).Activate

Next

ActiveSheet.Cells.AutoFilter

End Sub

·································································································

#如果数据比较多,要将as integer改为as long

利用excel拆分数据

标签:

原文地址:http://www.cnblogs.com/qiniqnyang/p/5906628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!