|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub Opiona()
- '禁止系统刷屏?触发其他事件等
- 'On Error Resume Next '// 发生错误,自动执行下一句,就是忽略错误
- Application.ScreenUpdating = False '//关闭屏幕刷新
- Application.DisplayAlerts = False '//关闭系统提示
- Application.EnableEvents = False '//禁止触发其他事件
- t = Timer '//开始时间
- BOOL标题 = True '//是否有标题行,如果有,只能占一行
-
- StrNames = "|" '//记录已经汇总的文件名
- Path来源 = ThisWorkbook.Path & "\数据"
- Path结果 = ThisWorkbook.Path & "\结果"
- Set FSO = CreateObject("Scripting.FileSystemObject")
- If FSO.FolderExists(Path结果) = True Then
- FSO.GetFolder(Path结果).Delete '//删除文件夹
- End If
-
- FileArr = FileAllArr(Path来源, "*.csv", ThisWorkbook.Name, True, False)
- MkDir Path结果 '//创建文件夹
-
- For I = 0 To UBound(FileArr)
-
- STRNEW = GetPathFromFileName(FileArr(I))
- If InStr(StrNames, "|" & STRNEW & "|") = 0 Then '//还未处理此文件名的文件
- ReDim ARX(1 To 1)
- ARX(1) = ""
- INTX = 1
- For X = 0 To UBound(FileArr)
- If STRNEW = GetPathFromFileName(FileArr(X)) Then
- ARX(INTX) = FileArr(X)
- INTX = INTX + 1
- ReDim Preserve ARX(1 To INTX)
- End If
- Next X
-
-
- If INTX > 2 Then '//说明有重复名
-
- Set WB = Workbooks.Add
- Set SHW = Worksheets(1) '//假设所有数据多在第一个表中,而且所有工作簿,此工作表名相同
-
- For X = 1 To UBound(ARX) - 1
-
- Set WB1 = Workbooks.Open(ARX(X))
- Set SHW1 = Worksheets(1) '//假设所有数据多在第一个表中,而且所有工作簿,此工作表名相同
-
- If BOOL标题 = True Then
- If X = 1 Then '//粘贴标题
- SHW1.Range("A1:AZ1").Copy SHW.Range("A1")
- End If
- Set tbl = SHW1.Range("A1").CurrentRegion
- SQLARR = tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count) '//去掉标题的区域
- Else
- SQLARR = SHW1.Range("A1").CurrentRegion
- End If
-
- IROW = SHW.Range("A65536").End(3).Row + 1
- SHW.Range("A" & IROW).Resize(UBound(SQLARR, 1), UBound(SQLARR, 2)) = SQLARR
-
- WB1.Close False
- Next X
-
- WB.SaveAs Filename:=Path结果 & "" & GetPathFromFileName(ARX(1)) & "_NEW.csv", FileFormat:=xlCSV
- WB.Close True
- Else
- PathNew = Path结果 & "" & GetPathFromFileName(FileArr(I), True)
- FileCopy FileArr(I), PathNew
- End If
- StrNames = StrNames & STRNEW & "|"
- End If
- Next
- Application.EnableEvents = True '// '//恢复触发其他事件
- Application.ScreenUpdating = True '//恢复屏幕刷新
- Application.DisplayAlerts = True '//恢复系统提示
- MsgBox "一共用时:" & Format(Timer - t, "#0.0000") & " 秒", , "北极狐提示!!" '//提示所用时间
- End Sub
复制代码 |
|