Loops 用于保护文件夹中xls中所有非空白单元格的宏
我需要一个宏打开文件夹中的所有工作簿,然后为每张工作表选择空白单元格,然后使其解锁,然后使用给定密码保护工作表 上面的代码仅对活动图纸执行此操作,如何使宏打开的所有图纸都执行此操作?我是否可以将下面的代码部署到代码中Loops 用于保护文件夹中xls中所有非空白单元格的宏,loops,excel,excel-2007,vba,Loops,Excel,Excel 2007,Vba,我需要一个宏打开文件夹中的所有工作簿,然后为每张工作表选择空白单元格,然后使其解锁,然后使用给定密码保护工作表 上面的代码仅对活动图纸执行此操作,如何使宏打开的所有图纸都执行此操作?我是否可以将下面的代码部署到代码中 Sub Divide() Dim fPath As String Dim fName As String Dim wb As Workbook Dim ws As Worksheet Dim pwd As String pwd = "can" ' Put your
Sub Divide()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim pwd As String
pwd = "can" ' Put your password here
'Setup
Application.ScreenUpdating = False
fPath = "C:\Documents and Settings\TRSECCAN\2011\Excel\" 'remember final \ in this string
fName = Dir(fPath & "*.xls") 'start a list of filenames
Do While Len(fName) > 0
Set wb = Workbooks.Open(fPath & fName) 'open found file
With ActiveSheet
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Locked = False
.Protect Password:=pwd
End With
wb.Close True 'close/save
fName = Dir 'get next filename
Loop
Application.ScreenUpdating = True
End Sub
提前感谢此代码将在活动工作簿中的每个工作表中循环显示工作表名称和单元格A1的值,并显示到即时窗口
UpdateLinks:=xlUpdateLinksNever
我自己不链接工作簿,因此无法帮助您回答这部分问题。以下是您的代码应该是什么样子(您应该删除不需要的
选择
:
Sub DisplayWSNames()
Dim InxWS As Integer
For InxWS = 1 To Sheets.Count
With Sheets(Inx)
Debug.Print "Cell A1 of Sheet " & .Name & " = " & .Cells(1, 1)
End With
Next
End Sub
有关更新链接和循环,请参见
我发现代码很有用,所以感谢大家的贡献工作手册上的括号。行需要移到末尾(例如,在UpdateLinks参数之后)。谢谢Rachel!(@RachelHettinger for notification:))谢谢大家,在你们的帮助下,我写了下面的代码。我把它作为一个answer@user768199:很高兴你终于来了。请不要忘记接受你自己的答案来“关闭”线程
Sub Divide()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim pwd As String
pwd = "can" ' Put your password here
'Setup
Application.ScreenUpdating = False
fPath = "C:\Documents and Settings\TRSECCAN\2011\Excel\" 'remember final \ in this string
fName = Dir(fPath & "*.xls") 'start a list of filenames
Do While Len(fName) > 0
Set wb = Workbooks.Open(fPath & fName, UpdateLinks:=xlUpdateLinksNever) 'open found file
For Each ws in wb.Worksheets
With ws
.SpecialCells(xlCellTypeBlanks).Locked = False
.Protect Password:=pwd
End With
Next ws
wb.Close True 'close/save
fName = Dir 'get next filename
Loop
Application.ScreenUpdating = True
End Sub
Sub Divide()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim pwd As String
pwd = "can" ' Put your password here
'Setup
Application.ScreenUpdating = False
fPath = "C:\Documents and Settings\TRSECCAN\2011\Excel\" 'remember final \ in this string
fName = Dir(fPath & "*.xls") 'start a list of filenames
Do While Len(fName) > 0
Set wb = Workbooks.Open(fPath & fName, UpdateLinks:=xlUpdateLinksNever) 'open found file
For Each ws In wb.Worksheets
With ws.Cells
.SpecialCells(xlCellTypeBlanks).Locked = False
End With
With ws
.Protect Password:=pwd
End With
Next ws
wb.Close True 'close/save
fName = Dir 'get next filename
Loop
Application.ScreenUpdating = True
End Sub