发布网友 发布时间:2022-05-03 01:50
共1个回答
热心网友 时间:2022-06-29 06:23
1.新建工作簿,复制进命名为“Sheet1”的表格,新建命名为“Sheet2”和”Sheet3”的空白表格
2.打开开发工具,进入VBA界面
3.把代码复制进去,ctrl+S,保存代码
Sub 导出数据()
'
' 导出数据 宏
'
'
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Range("A1:B1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$24").AutoFilter Field:=1, Criteria1:="1"
Range("A1:B6").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1:B24").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveSheet.Paste
Columns("B:B").ColumnWidth = 15.78
Sheets("Sheet3").Select
Application.CutCopyMode = False
Sheets("Sheet3").Move
ActiveWorkbook.SaveAs Filename:="C:\Users\Administrator\Desktop\导出数据1.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
ActiveSheet.Range("$A$1:$B$24").AutoFilter Field:=1, Criteria1:="2"
Range("A1:B24").Select
Selection.Copy
Sheets("Sheet4").Select
ActiveSheet.Paste
Sheets("Sheet4").Select
Application.CutCopyMode = False
Sheets("Sheet4").Move
ActiveWorkbook.SaveAs Filename:="C:\Users\Administrator\Desktop\导出数据2.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
ActiveSheet.Range("$A$1:$B$24").AutoFilter Field:=1, Criteria1:="3"
Range("A1:B24").Select
Selection.Copy
Sheets("Sheet5").Select
ActiveSheet.Paste
Sheets("Sheet5").Select
Application.CutCopyMode = False
Sheets("Sheet5").Move
ActiveWorkbook.SaveAs Filename:="C:\Users\Administrator\Desktop\导出数据3.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
ActiveSheet.Range("$A$1:$B$24").AutoFilter Field:=1, Criteria1:="4"
Range("A1:B24").Select
Selection.Copy
Sheets("Sheet6").Select
ActiveSheet.Paste
Sheets("Sheet6").Select
Application.CutCopyMode = False
Sheets("Sheet6").Move
ActiveWorkbook.SaveAs Filename:="C:\Users\Administrator\Desktop\导出数据4.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
3.点击运行,或者按F5,运行
4.桌面就会生成4个要分别导出的数据文件