excelvba遍历文件夹里的所有表格添加同一页

excelvba遍历文件夹里的所有表格添加同一页,第1张

import pandas as pd

import os

# 用os.walk遍历文件;用.endswith判断文件后缀

dfs = pd.DataFrame()

for root, dirs, files in os.walk(r'C:\Users\ll\Desktop\pandas\excels'):

for file in files:

if file.endswith('.xlsx'):

# 构建绝对路径

file_name = os.path.join(root, file)

# print(file_name)

df = pd.read_excel(file_name)

# print(df)

dfs = pd.concat([dfs, df])

# print(dfs)

dfs.to_excel(r'C:\Users\ll\Desktop\new.xlsx')

Public Sub 遍历()

Dim arr, brr

Application.ScreenUpdating = False

Application.DisplayAlerts = False

t = Timer

mypath = ThisWorkbook.Path &"\"

myname = Dir(mypath &"*.xls*")

ReDim brr(1 To 1000000, 1 To 6)

n = 1

Do While myname <>""

If myname <>ThisWorkbook.Name Then

Workbooks.Open (mypath &myname)

arr = ActiveSheet.Range("a1").CurrentRegion.Value

ActiveWorkbook.Close

For i = 1 To UBound(arr)

If arr(i, 5) = "签收" Or arr(i, 5) = "驿站代收" Or arr(i, 5) = "柜机代收" Then

For j = 1 To 6

brr(n, j) = arr(i, j)

Next

n = n + 1

End If

Next

End If

myname = Dir

Loop

ThisWorkbook.Sheets(1).[a1].Resize(1, 6) = arr

ThisWorkbook.Sheets(1).[a2].Resize(UBound(brr), 6) = brr

Columns(1).Select

Selection.TextToColumns Destination:=Selection, DataType:=xlDelimited, _

TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _

Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _

:=Array(1, 2), TrailingMinusNumbers:=True

MsgBox Timer - t

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11862784.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存