打开文件夹中的所有excel文件,执行操作并将其保存在同一文件夹中,并循环使用稍微更改的名称

打开文件夹中的所有excel文件,执行操作并将其保存在同一文件夹中,并循环使用稍微更改的名称,excel,vba,save-as,Excel,Vba,Save As,此代码将打开一个窗口,您可以在其中手动选择要打开的文件夹。之后,代码会依次打开该文件夹中的所有文件,并将其保存为名称稍有更改的新文件。我想做的是教它识别此时打开的文件,并根据文件名更改执行的操作 例如,3个文件存储在选定的文件夹中:“abc_2021”、“abc_total”和“aby” 我如何尝试解决我的问题: 将文件名值存储在字符串中 如果值的前5个符号为“abc_2”,则运行另一个宏(例如复制粘贴表格等),然后使用以下行将其另存为同一文件夹中的新文件 ActiveWorkbook.Save

此代码将打开一个窗口,您可以在其中手动选择要打开的文件夹。之后,代码会依次打开该文件夹中的所有文件,并将其保存为名称稍有更改的新文件。我想做的是教它识别此时打开的文件,并根据文件名更改执行的操作

例如,3个文件存储在选定的文件夹中:“abc_2021”、“abc_total”和“aby”

我如何尝试解决我的问题:

  • 将文件名值存储在字符串中
  • 如果值的前5个符号为“abc_2”,则运行另一个宏(例如复制粘贴表格等),然后使用以下行将其另存为同一文件夹中的新文件
  • ActiveWorkbook.SaveAs(myPath&myFile&Format(Now(),“DD-MMM-YYYY hh-mm”).xlsx”)

    如果值的前5个符号为“abc\t”,则在此处运行另一个宏,然后

    ActiveWorkbook.SaveAs(myPath&myFile&Format(Now(),“DD-MMM-YYYY hh-mm”).xlsx”)

    否则,运行第三个宏,并将其保存,并在其旁边附加适当的名称

    由于此操作是在循环中执行的,因此我将以正确的格式保存所有3个文件,并对每个文件执行正确的操作

    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    Dim flName As String, Searchtext As String
    Dim abc_2021 As String
    Dim abc_total As String
    Dim aby As String
    Dim Searchstring as String
    
    '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)
       
       'Pause Opened Workbook to do operaions before moving on to next line of code
         DoEvents
    
    如果有人需要代码的最后一部分,我将其附在下面:

      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
    

    这将有助于显示您尝试了什么,您得到的确切错误,以及在哪一行。我明白了(在leat)这里有两个问题1)
    ABC_202104_vs1.xls
    不像
    *.\ABC_2021#*.xls
    和2)如果将文件另存为同一目录,那么
    myFile=Dir
    会找到它们,你会有一个无休止的循环是的,我会将文件保存到一个新目录,谢谢。此外,有人告诉我,最大的问题不是生成所有文件的数组,这可以简化工作。代码将首先生成文件夹中所有文件的数组,然后根据文件名执行操作,最后将所有文件保存到新目录。
      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