|
楼主 |
发表于 2020-10-2 16:25
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim DataRows&
Dim arr
With CreateObject("Wscript.Shell")
ListFileArr = Split(.exec("cmd /c dir /a-d /b /s " & Chr(34) & ThisWorkbook.Path & Chr(34)).StdOut.ReadAll, vbCrLf)
ListFileArr = filter(ListFileArr, ".xl")这句话是不是指抓取数据工作簿格式XLS,XLSX都可以呢
End With
Set wb0 = ThisWorkbook
DataRows = Sheet1.Cells(Sheet1.Cells.Rows.Count, 2).End(xlUp).Row
If DataRows >= 2 Then Sheet1.Rows("2:" & DataRows).ClearContents
For i = 0 To UBound(ListFileArr)
DataRows = Val(Sheet1.Cells(Sheet1.Cells.Rows.Count, 2).End(xlUp).Row) + 1
mypath = ListFileArr(i)
TmpName = Split(mypath, "\")(UBound(Split(mypath, "\")))
If mypath <> ThisWorkbook.FullName And InStr(mypath, ".xlsm") = 0 Then
Set wb = Workbooks.Open(mypath)
Set sh = wb.Worksheets(1),如果要改为第二个工作表抓取数据,这里改为WORKSHEETS(2)
LastRow = sh.Cells(sh.Cells.Rows.Count, 1).End(xlUp).Row
arr = sh.Range("B2:F" & LastRow).Value
wb.Close
Sheet1.Cells(DataRows, 2).Resize(UBound(arr), UBound(arr, 2)).Value = arr
DataRowsE = Sheet1.Cells(Sheet1.Cells.Rows.Count, 2).End(xlUp).Row
Sheet1.Range("G" & DataRows & ":" & "G" & DataRowsE).Value = TmpName
End If
Next i
MsgBox "数据汇总完毕!"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
有几处颜色一致的,如果改为抓取第二个工作表的数据,就要将sheet1改为sheet2对吗,谢谢 |
|