vba - 使用 VBA 在工作簿文件夹中循环代码?

标签 vba loops excel

我有一个文件夹,其中包含许多格式相同的 Excel 文件。我修改了以下代码来确定日期并重新格式化它,其中“i”根据第 2 列的最后一行确定范围内的单元格数量。

Sub Test()
   Dim i As Long
   i = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
   With Range("K3:K" & i)
        .Formula = "=DATE(A3,G3,H3)"
        .NumberFormat = "ddmmmyyyy"
   End With  
End Sub

我想对文件夹中的所有工作簿执行此代码。我在 stackoverflow 上发现了以下问题:

Code for looping through all excel files in a specified folder, and pulling data from specific cells

它不会循环遍历我的所有文件,并且仅适用于我打开的第一个 Excel 文件。 如何在文件夹中的所有工作簿中循环此代码?以下是我到目前为止所拥有的。

Sub Test()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Dim i As Long

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch

            .LookIn = "C:\Test"
            .FileType = msoFileTypeExcelWorkbooks

                If .Execute > 0 Then
                    For lCount = 1 To .FoundFiles.Count

                        Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)

   i = wbResults.Worksheets("Sheet1").Cells(wbResults.Worksheets("Sheet1").Rows.Count, 2).End(xlUp).Row
   With wbResults.Worksheets("Sheet1").Range("K3:K" & i)
        .Formula = "=DATE(A3,G3,H3)"
        .NumberFormat = "ddmmmyyyy"
   End With

                        wbResults.Close SaveChanges:=False
                    Next lCount
                End If
        End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub

最佳答案

Excel 2007 及更高版本不支持

Application.FileSearch。试试这个代码( code for looping through files in a folder was taken from @mehow's site )

Sub PrintFilesNames()
    Dim file As String
    Dim wbResults As Workbook
    Dim i As Long
    Dim myPath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    myPath = "D:\" ' note, path ends with back slash

    file = Dir$(myPath & "*.xls*")

    While (Len(file) > 0)
        Set wbResults = Workbooks.Open(Filename:=myPath & file, UpdateLinks:=0)

        With wbResults.Worksheets(Split(file, ".")(0))
            i = .Cells(.Rows.Count, 2).End(xlUp).Row
            With .Range("K3:K" & i)
                 .Formula = "=DATE(A3,G3,H3)"
                 .NumberFormat = "ddmmmyyyy"
            End With
        End With

        wbResults.Close SaveChanges:=True
        'get next file
        file = Dir
    Wend

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

关于vba - 使用 VBA 在工作簿文件夹中循环代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022672/

相关文章:

excel - 解析字符串并对正负数字分量求和

excel - SSIS在有数据时将列读取为NULL

vba - Excel Addin 自动化运行问题

vba - 是否可以从excel创建可填写的PDF?

python - 增加字典中计数器的最快方法

c++ - 如何使用 CUDA Thrust 删除嵌套循环以进行全对距离检查?

php - 获取一列中的数据

vba - Excel VBA : sorting a table featuring cells with drop-down lists

mysql - 使用宏删除mysql表条目

PHP将用户数组插入用户数组并循环用户和用户数据(属性)