|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub VBA打开指定文件夹内所有Excel表格()
Application.ScreenUpdating = False
Set d = CreateObject("SCRIPTING.DICTIONARY")
Dim huoqulujing$, myPath$, myFile$, WB As Workbook '这个$ 是相当于定义字符串
w = InputBox("请输入要分析学科名称,如生物: ", , "生物") '输入要分析学科名称,为自定义排序提供数据
If w = "" Then End
Range("C1").Value = w
myPath = ThisWorkbook.Path & "\" '运用当前路径,即当前路径加一个反斜杠"
myFile = Dir(myPath & "*.xls*") '依次找寻当前路径中的*.xls,或者xlsx文件
[a1].CurrentRegion.Offset(1) = Empty
ar = Range("a1:d10000")
For j = 1 To UBound(ar, 2)
If Trim(ar(1, j)) <> "" Then
d(Trim(ar(1, j))) = j
End If
Next j
n = 1
Do While myFile <> "" '当指定路径中有文件时进行循环
If myFile <> ThisWorkbook.Name Then '如果我们这个宏文件在需要处理的文件夹之中,这个判断就会跳过下面的操作
Set WB = Workbooks.Open(myPath & myFile) '打开符合要求的文件
With WB.Worksheets(1)
br = .[a1].CurrentRegion
End With
WB.Close 0
For i = 2 To UBound(br)
If Trim(br(i, 1)) <> "" Then
n = n + 1
For j = 1 To UBound(br, 2)
lh = d(Trim(br(1, j)))
If lh <> "" Then
ar(n, lh) = br(i, j)
End If
Next j
End If
Next i
End If
myFile = Dir
Loop
[a1].Resize(n, UBound(ar, 2)) = ar
Set WB = Nothing '释放变量内存
Set d = Nothing '释放变量内存
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|