在Excel中的文件夹内的所有文件上添加一列

时间:2017-02-07 18:54:56

标签: excel vba excel-vba

我在一个文件夹(具有相同的布局)中有250个不同的Excel文件,列AF。我需要在列G上添加一个新列。传统方法是打开每个文件并在G添加新列。是否有使用Excel宏或任何其他工具完成此任务的简单过程?

2 个答案:

答案 0 :(得分:1)

This link帮助了我。以下是我的解决方案,有效:

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\dell\Desktop\Folder1\" '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

答案 1 :(得分:0)

是的,这是打开文件夹中所有Excel文件的代码:macro - open all files in a folder

在该循环中,您可以向该文件添加新列