|
- Option Explicit
- Sub 提取数据3()
- '---------------------------------------------------------------------------------------
- ' Procedure : 提取数据
- ' Author : hwc2ycy
- ' Date : 2012/12/23
- ' Purpose :
- '---------------------------------------------------------------------------------------
- Dim app As Object 'object,EXCEL实例
- Dim arr '数组
- Dim iRow&, iCol& '数据所在行
- Dim Filename$, Path$ '文件名,路径
- On Error Resume Next
- Application.ScreenUpdating = False '关闭刷屏
- Path = ThisWorkbook.Path & Application.PathSeparator
-
- '遍在文件
- Filename = Dir(Path & "*.xls*", vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
-
- If Len(Filename) > 0 Then
- Set app = CreateObject("excel.application") '创建对象
- '避免打开时运行宏
- app.AutomationSecurity = msoAutomationSecurityForceDisable
- app.DisplayAlerts = False '不显示错误
- End If
-
- Do While Len(Filename) > 0
- 'Debug.Print Filename
- If Not Filename Like "*" & ThisWorkbook.Name & "*" Then ' 避免打开本工作簿
- 'Debug.Print Filename & "已打开"
- With app.Workbooks.Open(Path & Filename, False, True)
- If Err.Number <> 0 Then MsgBox Filename & "打开失败": Err.Clear: GoTo error
- iCol = iCol + 1
- With .Worksheets(1)
- iRow = .Cells(65536, 1).End(xlUp).Row
- arr = .Range("d1:d" & iRow) '读取第4列数据
- End With
- .Close False
- End With
- iRow = Cells(65536, iCol).End(xlUp).Row '因为是03的格式,所以没有用ROWS.COUNT
- If iRow > 1 Then iRow = iRow + 1
- Cells(iRow, iCol).Resize(UBound(arr)) = arr '写入汇总表格
- End If
- error: Filename = Dir() '遍历
- Loop
-
- app.AutomationSecurity = msoAutomationSecurityByUI
- app.DisplayAlerts = True
- Set app = Nothing
- Application.ScreenUpdating = True
- MsgBox "提取完成"
- End Sub
复制代码 分列存数据。 |
|