如何锁定具有子文件夹的文件夹中所有文件的单元格

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

我有100个excel文件。它们在子文件夹中。每个子文件夹都有10-15个excel文件。

我想为子文件夹中的所有100个文件锁定单元格A1:A10。

我使用过VBA。

例如,这些是不同的路径,C:\ Users \ mmishal001 \ Desktop \ Project PT Attempt 3 \ DEMO2 for VBA \ Director 1 \ Manager 1C:\ Users \ mmishal001 \ Desktop \ Project PT Attempt 3 \ DEMO2 for VBA \ Director 1 \ Manager 2C:\ Users \ mmishal001 \ Desktop \ Project PT Attempt 3 \ DEMO2 for VBA \ Director 2 \ Manager 3C:\ Users \ mmishal001 \ Desktop \ Project PT Attempt 3 \ DEMO2 for VBA \ Director 2 \ Manager 4

每个都有10-15个文件。

我已经使用下面的代码写入文件了,如果您可以编辑以下内容以锁定上述子文件夹中所有excel文件的单元格A1:A10,而无需再次编写函数(可能会循环),将不胜感激。 ?)

Sub TextInAll()
Dim my_files As String
Dim folder_path As String
Dim subfolder As String
Dim wb As Workbook
Dim ws As Worksheet
'Assign path to variable
folder_path = "C:\Users\mmishal001\Desktop\Project PT Attempt 3\DEMO2 for 
VBA\Director 1\Manager 1"
'specifying file types or extn.
my_files = Dir(folder_path & "\*.xlsx")
Do While my_files <> vbNullString
    Set wb = Workbooks.Open(folder_path & "\" & my_files)
    Set ws = wb.Sheets(1)
    ws.Range("A1:A5").Value = "mahir"
    wb.Close True
    my_files = Dir()
Loop
MsgBox ("All files are updated")
End Sub

我希望其中一个代码可以运行。当我转到子文件夹中的100个文件中的任何一个时,单元格区域A1:A10被锁定在每个文件中。

excel vba subdirectory
1个回答
0
投票

我以下列方式理解它。像这样修改您的Do Loop

Do While my_files <> vbNullString
    Set wb = Workbooks.Open(folder_path & "\" & my_files)
    Set ws = wb.Sheets(1)
    ws.Range("A1:A5").Value = "mahir"

    ' This is the code to insert
    With ws
        .Cells.Locked = True
        .Range("A1:A10").Locked = False
        .Protect ""  'No password but protected. 
    End With

    wb.Close True
    my_files = Dir()
Loop

更新:根据信息,您可以执行类似的操作

Sub TextInAll()
    Dim my_files As String
    Dim folder_path As Variant
    Dim subfolder As String
    Dim wb As Workbook
    Dim ws As Worksheet

    Dim vFiles As Variant
    vFiles = Array("C:\Users\mmishal001\Desktop\Project PT Attempt 3\DEMO2 for VBA\Director 1\Manager 1", _
        "C:\Users\mmishal001\Desktop\Project PT Attempt 3\DEMO2 for VBA\Director 1\Manager 2", _
        "C:\Users\mmishal001\Desktop\Project PT Attempt 3\DEMO2 for VBA\Director 2\Manager 3", _
        "C:\Users\mmishal001\Desktop\Project PT Attempt 3\DEMO2 for VBA\Director 2\Manager 4")


    For Each folder_path In vFiles
        'specifying file types or extn.
        my_files = Dir(folder_path & "\*.xlsx")
        Do While my_files <> vbNullString
            Set wb = Workbooks.Open(folder_path & "\" & my_files)
            Set ws = wb.Sheets(1)

            ' This is the code to insert
            With ws
                .Cells.Locked = True
                .Range("A1:A10").Locked = False
                .Protect ""  'No password but protected.
            End With

            ws.Range("A1:A5").Value = "mahir"
            wb.Close True
            my_files = Dir()
        Loop
    Next folder_path
    MsgBox ("All files are updated")
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.