將excel按照某一列拆分成多個文件


1.alt+F11

2.點擊

 

 3.拷貝代碼

代碼:

Sub NewSheets()

Dim d As Object, sht As Worksheet, arr, brr, r, kr, i&, j&, k&, x&

Dim Rng As Range, Rg As Range, tRow&, tCol&, aCol&, pd&

Application.ScreenUpdating = False

Application.DisplayAlerts = False

Set d = CreateObject("scripting.dictionary")

Set Rg = Application.InputBox("請您框選拆分依據列!只能選擇單列單元格區域!", title:="提示", Type:=8)

tCol = Rg.Column

tRow = Val(Application.InputBox("請您輸入總表標題行的行數?"))

If tRow = 0 Then MsgBox "您未輸入標題行行數,程序退出!": Exit Sub

Set Rng = ActiveSheet.UsedRange

arr = Rng

tCol = tCol - Rng.Column + 1

aCol = UBound(arr, 2)

For i = tRow + 1 To UBound(arr)

If Not d.exists(arr(i, tCol)) Then

d(arr(i, tCol)) = i

Else

d(arr(i, tCol)) = d(arr(i, tCol)) & "," & i

End If

Next

For Each sht In Worksheets

If d.exists(sht.Name) Then sht.Delete

Next

kr = d.keys

For i = 0 To UBound(kr)

If kr(i) <> "" Then

r = Split(d(kr(i)), ",")

ReDim brr(1 To UBound(r) + 1, 1 To aCol)

k = 0

For x = 0 To UBound(r)

k = k + 1

For j = 1 To aCol

brr(k, j) = arr(r(x), j)

Next

Next

With Worksheets.Add(, Sheets(Sheets.Count))

.Name = kr(i)

.[a1].Resize(tRow, aCol) = arr

.[a1].Offset(tRow, 0).Resize(k, aCol) = brr

Rng.Copy

.[a1].PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

.[a1].Select

End With

End If

Next

Sheets(1).Activate

Set d = Nothing

Erase arr: Erase brr

MsgBox "數據拆分完成!"

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

 4.保存

5.使用

 

 

 

 

 

 

 

 參考資料:1.https://baijiahao.baidu.com/s?id=1638300768682628867&wfr=spider&for=pc

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM