将边框应用于文件夹中所有Excel文件的宏

时间:2020-10-19 14:03:03

标签: excel vba

我对VBA / MACRO非常陌生,我正在尝试创建一个宏,该宏将Border应用于文件夹中存在的所有excel文件。

1 个答案:

答案 0 :(得分:0)

尝试一下:

    Dim FolderPath As String
    Dim StrFile As String
    
    ' Put your folder path here
    FolderPath = "C:\Temp"
    
    Dim xlWB As Excel.Workbook, xlWS As Excel.Worksheet
    
    StrFile = Dir(FolderPath)
    
    Do While Len(StrFile) > 0
        ' Basic check that this is a spreadsheet
        If Right(StrFile, 5) = ".xlsx" Then
            Set xlWB = Workbooks.Open(FolderPath & "/" & StrFile)
            
            For Each xlWS In xlWB.Worksheets
                If xlWS.Name = "POL" Or xlWS.Name = "POD" Then
                    With xlWS.UsedRange
                        With .Borders(xlEdgeLeft)
                            .LineStyle = xlContinuous
                            .ColorIndex = xlAutomatic
                            .Weight = xlThick
                        End With
                    End With
                End If
            Next
            
            xlWB.Close SaveChanges:=True
        End If
        
        StrFile = Dir()
    
    Loop

您应该考虑更好地进行错误检查和验证,以确保所找到的文件实际上是Excel工作簿。