1. Sub MergeFile()
    2. Dim MyPath$, MyName$, sht As Worksheet, SaveFileName$, Extension$, WshShell As Object
    3. ' Set MyPath = ThisWorkbook.Path & "\"
    4. SaveFileName = Format(Now(), "yyyy_MM_dd_HH_mm_ss")
    5. Application.CutCopyMode = False
    6. 100:
    7. Extension = InputBox("请输入要合并的文件的扩展名:" & vbNewLine & "xlsx" & vbNewLine & "csv" & vbNewLine & "xls", "请输入文件类型", "xlsx")
    8. Extension = LCase(Trim(Extension))
    9. If (StrComp(Extension, "xlsx", 1) = 0) Xor (StrComp(Extension, "csv", 1) = 0) Xor (StrComp(Extension, "xls", 1) = 0) Then
    10. Else
    11. GoTo 100
    12. End If
    13. 200:
    14. MyPath = InputBox("请输入要合并的文件的文件夹绝对路径:", "请输入文件夹路径", ThisWorkbook.Path)
    15. MyPath = Trim(MyPath)
    16. ' Dim fso As New Scripting.FileSystemObject
    17. Set fso = CreateObject("Scripting.FileSystemObject")
    18. If Not fso.folderexists(MyPath) Then
    19. GoTo 200
    20. End If
    21. Set fso = Nothing
    22. MyPath = MyPath & "\"
    23. MyName = Dir(MyPath & "*." & Extension)
    24. Application.ScreenUpdating = False
    25. Do While Len(MyName) > 0
    26. If MyName <> ThisWorkbook.Name Then
    27. With GetObject(MyPath & MyName)
    28. For Each sht In .Sheets
    29. sht.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    30. Next
    31. .Close False
    32. End With
    33. End If
    34. MyName = Dir
    35. Loop
    36. Application.DisplayAlerts = False
    37. Application.CutCopyMode = False
    38. SaveFileName = MyPath & SaveFileName & "." & Split(ActiveWorkbook.Name, ".")(UBound(Split(ActiveWorkbook.Name, ".")))
    39. ActiveWorkbook.SaveAs SaveFileName, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
    40. Application.DisplayAlerts = True
    41. MsgBox "已合并" & vbNewLine & "并保存!", 64, "完成"
    42. ActiveWorkbook.Close False
    43. Application.DisplayAlerts = False
    44. Application.Quit
    45. Set WshShell = CreateObject("WScript.Shell")
    46. WshShell.Run "taskkill /f /im EXCEL.exe"
    47. Set WshShell = Nothing
    48. End Sub
    49. Sub SplitFile()
    50. Dim MyPath$, sht As Worksheet, SaveFileName$, Extension$, WshShell As Object, MyFileFormat As XlFileFormat
    51. Application.CutCopyMode = False
    52. 100:
    53. Extension = InputBox("请输入要拆分后的文件的扩展名:" & vbNewLine & "xlsx" & vbNewLine & "csv" & vbNewLine & "xls", "请输入文件类型", "xlsx")
    54. Extension = LCase(Trim(Extension))
    55. If (StrComp(Extension, "xlsx", 1) = 0) Xor (StrComp(Extension, "csv", 1) = 0) Xor (StrComp(Extension, "xls", 1) = 0) Then
    56. Else
    57. GoTo 100
    58. End If
    59. Select Case Extension
    60. Case "csv"
    61. MyFileFormat = XlFileFormat.xlCSV
    62. Case "xlsx"
    63. MyFileFormat = XlFileFormat.xlOpenXMLWorkbook
    64. Case "xls"
    65. MyFileFormat = XlFileFormat.xlExcel8
    66. Case Else
    67. MyFileFormat = XlFileFormat.xlOpenXMLWorkbook
    68. End Select
    69. 200:
    70. MyPath = InputBox("请输入要拆分的文件的文件夹绝对路径:", "请输入文件夹路径", ThisWorkbook.Path)
    71. MyPath = Trim(MyPath)
    72. Set fso = CreateObject("Scripting.FileSystemObject")
    73. If Not fso.folderexists(MyPath) Then
    74. GoTo 200
    75. End If
    76. Set fso = Nothing
    77. MyPath = MyPath & "\"
    78. Application.ScreenUpdating = False
    79. Application.DisplayAlerts = False
    80. Application.CutCopyMode = False
    81. For Each sht In Sheets
    82. sht.Copy
    83. SaveFileName = MyPath & sht.Name & "." & Extension
    84. ActiveWorkbook.SaveAs SaveFileName, FileFormat:=MyFileFormat, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
    85. ActiveWorkbook.Close True
    86. Next
    87. Application.DisplayAlerts = True
    88. MsgBox "已拆分" & vbNewLine & "并保存!", 64, "完成"
    89. ActiveWorkbook.Close False
    90. Application.DisplayAlerts = False
    91. Application.Quit
    92. Set WshShell = CreateObject("WScript.Shell")
    93. WshShell.Run "taskkill /f /im EXCEL.exe"
    94. Set WshShell = Nothing
    95. End Sub