调整代码以查找初学者的子文件夹

时间:2019-01-30 22:08:12

标签: excel vba

我对excel宏非常陌生。我有下面的宏,它可以完全满足我的需要,但它仅显示在我选择的文件夹中,而不显示在其子文件夹中。我有一个很好的谷歌,但真的很难适应它,所以它可以工作。任何指针或帮助将不胜感激。

我已经看到很多其他人将子文件夹添加到那里的宏,但是我似乎无法适应以下情况。我还有很多其他的宏,但是如果我可以在下面的宏中使用它,则可以在其余的所有宏中使用。加上它也是很好的学习练习


Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim LastRow As Long
Dim i As Long

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  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

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)

    'Ensure Workbook has opened before moving on to next line of code
      DoEvents

    'Change First Worksheet's Background Fill Blue

    With wb
    wb.Worksheets("Tests").Select
    LastRow = Range("E" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Range("E" & i).Value = """fixednavigation22""" Then
            Range("D" & i).Value = "Click by id"
              End If
        Next i
    End With

    'Save and Close Workbook
      wb.Close SaveChanges:=True

    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    'Get next file name
      myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub```

0 个答案:

没有答案