繁体   English   中英

如何使用 VBA Excel 忽略文件夹中的特定工作簿?

[英]How to ignore specific workbook in folder using VBA Excel?

我有一个宏,让我 select 我想要的文件夹然后我有一个循环打开其中的所有 excel 文件但我想排除这个工作簿(Dummy.xlsm)。 循环浏览文件夹时是否可以不读取该文件?

这是代码。 非常感谢!

Dim wb As Workbook
Dim ws As Worksheet
Dim ValueRow As Integer
Dim ValueColumn As Integer
Dim LookupValue As Variant
Dim ValueAtColumn1 As Variant
Dim ValueAtColumn2 As Variant
Dim ValueAtColumn3 As Variant
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim cel As Range
Dim arrayString() As String
Dim ppmString() As String
Dim StrFile As String

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

myExtension = "*.xls*"
myFile = Dir(myPath & myExtension)

Do While myFile <> ""
Set wb = Workbooks.Open(FileName:=myPath & myFile)
ValueRow = 4
Sheets("Template").Range("A4:IX200").ClearContents

DoEvents

For Each ws In Sheets
Select Case ws.Name
Case Is = "Summary", "Count", "Table", "Sheet1", "Sheet2", "Template", "Template(2)", "Control", "Generated_data_2", "Fleet Data"
Case Else
ws.Range("C3").Copy Worksheets("Template").Cells(Rows.Count, 1).End(xlUp).Offset(1)
For i = 4 To 6
    Worksheets("Template").Cells(ValueRow, i).Value = ws.Range(Worksheets("Template").Cells(3, i).Value).Value
Next i

Next ws

wb.Close SaveChanges:=True
DoEvents
myFile = Dir
Loop
MsgBox "Done!"

ResetSettings:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

您可以在所有不想运行的代码周围使用If语句来检查文件名,如下所示:

myFile = Dir(myPath & myExtension)

Do While myFile <> ""
  If myFile <> "Dummy.xlsm" Then
    Set wb = Workbooks.Open(FileName:=myPath & myFile)

    ' the rest of the code here

    wb.Close SaveChanges:=True    
  End If
  myFile = Dir
Loop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM