1.利用Excel中開發工具[https://blog.csdn.net/ntotl/article/details/79141314](https://blog.csdn.net/ntotl/article/details/79141314)
(1)打開需要拆分的Excel檔案,Alt+F11打開VBE視窗;
(2)插入——模塊,輸入代碼:
Sub 保留表頭拆分資料為若干新作業簿()
Dim arr, d As Object, k, t, i&, lc%, rng As Range, c%
c = Application.InputBox("請輸入拆分列號", , 4, , , , , 1)
If c = 0 Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = [a1].CurrentRegion
lc = UBound(arr, 2)
Set rng = [a1].Resize(, lc)
Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, c)) Then
Set d(arr(i, c)) = Cells(i, 1).Resize(1, lc)
Else
Set d(arr(i, c)) = Union(d(arr(i, c)), Cells(i, 1).Resize(1, lc))
End If
Next
k = d.Keys
t = d.Items
For i = 0 To d.Count - 1
With Workbooks.Add(xlWBATWorksheet)
rng.Copy .Sheets(1).[a1]
t(i).Copy .Sheets(1).[a2]
.SaveAs Filename:=ThisWorkbook.Path & "\" & k(i) & ".xls"
.Close
End With
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完畢"
End Sub
轉自https://blog.csdn.net/ntotl/article/details/79141314代碼,
注意分拆分后的Excel檔案格式為xls,
(3)開發工具——插入——按鈕


會形成一個按鈕,點擊該表單控制元件,加載剛剛建立的宏,設定拆分序列號,即根據哪一列屬性拆分(class),
(4)完成操作
2.VBA批量將xls檔案轉換成xlsx檔案
(1)新建一個Excel(與需要轉換檔案同一目錄);
(2)輸入代碼:
Sub xls2xlsx()
Dim FilePath, MyFile, iPath, Name, OutPath As String
iPath = ThisWorkbook.Path
OutPath = Dir(iPath & "\xlsx", vbDirectory)
If OutPath = "" Then
MkDir (iPath & "\xlsx")
End If
MyFile = Dir(iPath & "\*.xls")
If MyFile <> "" Then
Do
On Error Resume Next
If MyFile = ThisWorkbook.Name Then MyFile = Dir
Workbooks.Open (iPath & "\" & MyFile)
MyFile = Replace(MyFile, ".xls", ".xlsx")
Name = "\" & MyFile
FilePath = iPath & "\xlsx" & Name
Application.ScreenUpdating = False
ActiveWorkbook.SaveAs Filename:=FilePath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Workbooks(MyFile).Close True
Application.ScreenUpdating = True
MyFile = Dir
Loop While MyFile <> ""
End If
End Sub
運行結束!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379455.html
標籤:其他
上一篇:編程人員常用工具軟體
