如何锁定具有子文件夹的文件夹中所有文件的单元格
How to lock cells for all files in a folder which has sub-folders
我有 100 个 excel 个文件。它们位于子文件夹中。每个子文件夹有 10-15 excel 个文件。
我想为子文件夹中的所有 100 个文件锁定单元格 A1:A10。
我用过VBA。
例如,这些是不同的路径,
C:\Users\mmishal001\Desktop\Project PT 尝试 3\DEMO2 for VBA\Director 1\Manager 1
C:\Users\mmishal001\Desktop\Project PT 尝试 3\DEMO2 for VBA\Director 1\Manager 2
C:\Users\mmishal001\Desktop\Project PT 尝试 3\DEMO2 for VBA\Director 2\Manager 3
C:\Users\mmishal001\Desktop\Project PT 尝试 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 在每个文件中都被锁定。
我是这样理解的。只需像这样修改您的 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
更新:根据 post 的信息,您可以做类似的事情
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
我有 100 个 excel 个文件。它们位于子文件夹中。每个子文件夹有 10-15 excel 个文件。
我想为子文件夹中的所有 100 个文件锁定单元格 A1:A10。
我用过VBA。
例如,这些是不同的路径, C:\Users\mmishal001\Desktop\Project PT 尝试 3\DEMO2 for VBA\Director 1\Manager 1 C:\Users\mmishal001\Desktop\Project PT 尝试 3\DEMO2 for VBA\Director 1\Manager 2 C:\Users\mmishal001\Desktop\Project PT 尝试 3\DEMO2 for VBA\Director 2\Manager 3 C:\Users\mmishal001\Desktop\Project PT 尝试 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 在每个文件中都被锁定。
我是这样理解的。只需像这样修改您的 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
更新:根据 post 的信息,您可以做类似的事情
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