|
- Sub find()
- Rem 文件路径
- Dim pth As String
- pth = ThisWorkbook.Path & ""
-
- Rem 当前文件名
- Dim myname As String
- myname = ThisWorkbook.Name
-
- Rem 获取文件名
- Dim arr(), a As Integer, flname As String
- flname = Dir(pth) '获取文件名
- Do While flname <> ""
- Rem 不是当前文件,就是需要处理的文件
- If flname <> myname Then
- a = a + 1 '文件数量加1
- ReDim Preserve arr(1 To a) '重定义数组大小,并保留数据
- arr(a) = flname '文件名存入数组
- End If
- flname = Dir '继续获取文件名
- Loop
-
- Rem 没有文件则退出
- If a = 0 Then
- MsgBox "未发现文件!"
- Exit Sub
- End If
-
- Rem 条件数组
- Dim brr
- brr = Range("G2:G5").Value
-
- Rem 结果数组
- Dim crr(), i As Long
- ReDim crr(1 To a, 1 To 4)
- For i = 1 To a
- crr(i, 1) = arr(i) '文件名存入数组
- Next
-
- Application.ScreenUpdating = False '禁用屏幕刷新
-
- Rem 要提取的数据存入数组
- Dim sht As Worksheet, j As Integer
- For i = 1 To a
- Workbooks.Open pth & arr(i), False '打开文件且不更新
- With ActiveWorkbook
- On Error Resume Next '出现错误则继续
-
- Rem 变量赋值,判断工作表是否存在
- Set sht = .Sheets(brr(1, 1))
-
- Rem 文件存在(没有错误)则处理,不存在则下一个
- If Err.Number = 0 Then
- With sht
- For j = 2 To 4
- crr(i, j) = .Range(brr(j, 1)).Value '提取数据
- Next j
- End With
- Else
- Err.Clear '清除错误,继续处理下一个
- End If
-
- On Error GoTo 0 '恢复错误处理(出现其他错误时提示)
-
- .Close False '关闭文件且不保存
- End With
- Next i
-
- Call scsj '调用过程,清除原有数据
- Range("A3").Resize(a, 4).Value = crr '输入提取结果
-
- Application.ScreenUpdating = True '恢复屏幕刷新
-
- MsgBox "处理完成!"
- End Sub
复制代码
|
|