打开多个Excel文件,并添加新细胞

问题描述 投票:0回答:1

我试图打开多个Excel文件,并具有相同名称的每个添加相同的新细胞。他们是一个文件夹... /桌面/ EXCEL命名workbook1,workbook2等。

我试图this article已经但是我得到一个运行时错误76“找不到路径”。

screenshot

我是超级新手用VBA,任何帮助表示赞赏!这是我在运行该脚本:

Sub LoopThroughFolder()

Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range
Set Wb = ThisWorkbook
'change the address to suite
MyDir = "C:\Users\shaye\Desktop\excel" 'Your Directory
MyFile = Dir(MyDir & "*.xlsx")    'Your excel file extension
ChDir MyDir
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0

Do While MyFile <> ""
    Workbooks.Open (MyFile)
        Range("G1").Value = "NewColumn" 'New Column Name
        ActiveWorkbook.Save
        ActiveWorkbook.Close True
    MyFile = Dir()
Loop

End Sub

[

excel vba
1个回答
1
投票

试试这个代码。我认为你需要在你的目录本“\”和“?”在你的文件扩展名来找到几个Excel的类型

Sub LoopThroughFolder()

Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range
Set Wb = ThisWorkbook
'change the address to suite
MyDir = "C:\Users\shaye\Desktop\excel\" 'Your Directory need this "\"
MyFile = Dir(MyDir & "*.xl??")    'Your excel file extension
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0

Do While MyFile <> ""
    Workbooks.Open (MyFile)
        Range("G1").Value = "NewColumn" 'New Column Name
        ActiveWorkbook.Save
        ActiveWorkbook.Close True
    MyFile = Dir()
Loop

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.