![](/img/trans.png)
[英]Excel VBA store only selected Files in Array (not all in folder) and loop through them if at least one file was selected
[英]Excel VBA Ignore one file whilst using Loop through a folder
我有一个宏可以找到一个文件夹并以相同的方式更新其中的所有 Excel 文档,但是我需要它忽略一个但我无法实现它。
这是宏:
Sub FileUpdate()
Application.AskToUpdateLinks = False
Application.ScreenUpdating = False
Application.Calculation = xlManual
FolderName = (Sheets("Filepaths").Range("c22"))
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Filename = Dir(FolderName & ".xlsx")
Do While Len(Filename)
With Workbooks.Open(FolderName & Filename)
Call SheetUpdate
End With
Filename = Dir
ActiveWorkbook.Close SaveChnges = True
Loop
Application.AskToUpdateLinks = True
Application.Calculation = xlAutomatic
End Sub
这些文件都需要放在同一个文件夹中,所以不幸的是只有一个文件不能让这个宏在其中运行。
为了以防万一,文件夹的文件路径位于名为“Filepaths”的工作表中,它调用另一个名为“SheetUpdate”的宏。
我曾尝试使用“If Not”来让它向后运行 - 因此它更新了所有没有我想要避免的文件名称的文档。 我也尝试过以不同的变体使用“Case”,但这更多是尝试将我见过的其他东西放在一起。
我对此非常陌生,我已经做了很多寻找答案的工作,但我正在为这个特定的问题而苦苦挣扎。
根据您的描述,尚不完全清楚SheetUpdate
工作原理,但您似乎正在使用ActiveSheet
引用。 如果您打算将来继续使用 VBA,您可能会对这篇文章感兴趣。
可能适合您的解决方案如下所示
Sub FileUpdate()
Application.AskToUpdateLinks = False
Application.ScreenUpdating = False
Application.Calculation = xlManual
Dim filderName As String
folderName = ThisWorkbook.workshSheets("Filepaths").Range("c22").value
If Right(folderName, 1) <> Application.PathSeparator Then _
folderName = folderName & Application.PathSeparator
Dim fileName As String
fileName = Dir(folderName, vbNormal)
Do While fileName <> ""
If Not fileName = "name of file to ignore" Then
With Workbooks.Open(folderName & fileName)
Call SheetUpdate
End With
ActiveWorkbook.Close SaveChanges = True
End If
fileName = Dir
Loop
Application.AskToUpdateLinks = True
Application.Calculation = xlAutomatic
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.