本文介绍了在一个文件夹中的几个文件上运行宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个宏,我想在一个文件夹中的多个文件上运行。我怎么能这样做?

I created a macro which I want to run on multiple files in one folder. How can I do this?

Sub Macro1()

'

'Macro1 Macro

'

'键盘快捷键:Ctrl + e

'

   列("W:W")。选择

    Selection.Delete Shift:= xlToLeft

   列("Y:Y")。选择

    Selection.Delete Shift:= xlToLeft

   范围("A1")。选择

结束子

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+e
'
    Columns("W:W").Select
    Selection.Delete Shift:=xlToLeft
    Columns("Y:Y").Select
    Selection.Delete Shift:=xlToLeft
    Range("A1").Select
End Sub

推荐答案

选项显式

Sub OpenMultipleUserSelectedFiles()

     Dim FileArray As Variant

   将我的书作为工作簿变暗

    Dim i As Integer

    FileArray = Application.GetOpenFilename(,," Select the Files",MultiSelect:= True)

   如果是IsArray(FileArray)然后是
       对于i = LBound(FileArray)至UBound(FileArray)

           设置myBook = Workbooks.Open(FileArray(i))

           列("W:W")。删除Shift:= xlToLeft

           列("Y:Y")。删除Shift:= xlToLeft

           范围("A1")。选择

            myBook.Save

            myBook.Close

       接下来我是
   否则:
$
        MsgBox"您点击取消"
$
   结束如果

结束子

Sub OpenMultipleUserSelectedFiles()
    Dim FileArray As Variant
    Dim myBook As Workbook
    Dim i As Integer
    FileArray = Application.GetOpenFilename(, , "Select the Files", MultiSelect:=True)
    If IsArray(FileArray) Then
        For i = LBound(FileArray) To UBound(FileArray)
            Set myBook = Workbooks.Open(FileArray(i))
            Columns("W:W").Delete Shift:=xlToLeft
            Columns("Y:Y").Delete Shift:=xlToLeft
            Range("A1").Select
            myBook.Save
            myBook.Close
        Next i
    Else:
        MsgBox "You clicked cancel"
    End If
End Sub

 


这篇关于在一个文件夹中的几个文件上运行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:30