Namig: drugi jeziki so prevedeni v Google. Lahko obiščete English različico te povezave.
Vpiši se
x
or
x
x
Registracija
x

or

Kako zagnati makro ob istem času v več datotekah delovne knjige?

V tem članku bom govoril o tem, kako zagnati makro v več datotekah delovne knjige hkrati, ne da bi jih odpiral. Naslednja metoda vam lahko pomaga rešiti to nalogo v Excelu.

Zaženite makro enako v več delovnih zvezkih s kodo VBA


Zaženite makro enako v več delovnih zvezkih s kodo VBA

Če želite zagnati makro v več delovnih zvezkih, ne da bi jih odprli, uporabite naslednjo kodo VBA:

1. Drži dol ALT + F11 tipke za odpiranje Microsoft Visual Basic za aplikacije okno.

2. Kliknite Vstavi > Moduliin prilepite naslednji makro v Moduli Okno.

VBA koda: istočasno zaženite isti makro v več delovnih zvezkih:

Sub LoopThroughFiles()
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)
                'your code here
            End With
            xFileName = Dir
        Loop
    End If
End Sub

Opombe: V zgoraj navedeni kodi kopirajte in prilepite svojo kodo brez spodaj naslov in End Sub nogo med Z delovnimi knjigami.Open (xFdItem & xFileName) in Končaj s skripte. Prikaz slike:

doc zaženite makro več datotek 1

3. Nato pritisnite F5 ključ za izvedbo te kode in a Brskanje prikaže se okno, izberite mapo, v kateri so delovni zvezki, za katere želite, da se vsi uporabljajo za to makro, si oglejte posnetek zaslona:

doc zaženite makro več datotek 2

4. In nato kliknite OK gumb, se želeni makro izvede naenkrat iz enega delovnega zvezka na druge.


Priporočena orodja za produktivnost za Excel

zavihek kte 201905

Kutools za Excel vam pomaga, da vedno končate delo pred časom in izstopite iz množice

  • Več kot zmogljive napredne funkcije 300, zasnovane za 1500 delovne scenarije, ki povečujejo produktivnost z 70%, vam dajejo več časa za skrb za družino in uživanje v življenju.
  • Ne potrebujete več pomnilniških formul in VBA kod, od zdaj naprej pa dajate svojim možganom počitek.
  • Postanite strokovnjak za Excel v minutah 3, zapletene in ponavljajoče se operacije lahko opravite v nekaj sekundah,
  • Vsak dan zmanjšajte število operacij tipkovnice in miške, zdaj se poslovite od poklicnih bolezni.
  • 110,000 visoko učinkovite ljudi in 300 + svetovno priznanih podjetij izbiro.
  • Brezplačna preizkusna različica 60 dneva. 60-dnevno jamstvo vračila denarja. 2 let brezplačne nadgradnje in podpore.

Prinaša kartično brskanje in urejanje za Microsoft Office, veliko močnejši od zavihkov brskalnika

  • Office Tab je namenjen za Word, Excel, PowerPoint in druge Office aplikacije: Založnik, Dostop, Visio in Projekt.
  • Odprite in ustvarite več dokumentov v novih zavihkih istega okna in ne v novih oknih.
  • Z 50% poveča vašo produktivnost in vsak dan zmanjša na stotine klikov z miško!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Joel · 2 months ago
    Very useful macro, and it works great, but I would like to be able to select which files from that folder I want the macro to be ran on? For example I have 4 files in a folder with other excel files and I only want it ran on those 4 specific files. How can I tweak your macro to let me pick those 4 files from that folder?
    • To post as a guest, your comment is unpublished.
      skyyang · 2 months ago
      Hi, Joel,
      To trigger the same code in specific workbooks, you should apply the below code:

      Sub LoopThroughFiles()
      Dim xFd As FileDialog
      Dim xFdItem As Variant
      Dim xFileName As String
      Dim xFB As String
      With Application.FileDialog(msoFileDialogOpen)
      .AllowMultiSelect = True
      .Filters.Clear
      .Filters.Add "excel", "*.xls*"
      .Show
      If .SelectedItems.Count < 1 Then Exit Sub
      For lngCount = 1 To .SelectedItems.Count
      xFileName = .SelectedItems(lngCount)
      If xFileName <> "" Then
      With Workbooks.Open(Filename:=xFileName)
      'your code
      End With
      End If
      Next lngCount
      End With
      End Sub

      Please try it, hope it can help you!
  • To post as a guest, your comment is unpublished.
    yarto logistics · 2 months ago
    I followed instructions but get a compile error "Loop wihtout Do". What am i missing? My macro code is very simple just change font size of specified rows. Works by it self. Here is what I have... please help

    Sub LoopThroughFiles()
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
    xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
    xFileName = Dir(xFdItem & "*.xls*")
    Do While xFileName <> ""
    With Workbooks.Open(xFdItem & xFileName)
    'your code here
    Rows("2:8").Select
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .Color = -11518420
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With
    xFileName = Dir
    Loop
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      skyyang · 2 months ago
      Hello, yarto,
      You missed the "End with" script at the end of your code, the correct one should be this:
      Sub LoopThroughFiles()
      Dim xFd As FileDialog
      Dim xFdItem As Variant
      Dim xFileName As String
      Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
      If xFd.Show = -1 Then
      xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
      xFileName = Dir(xFdItem & "*.xls*")
      Do While xFileName <> ""
      With Workbooks.Open(xFdItem & xFileName)
      'your code here
      Rows("2:8").Select
      With Selection.Font
      .Name = "Arial"
      .Size = 16
      .Strikethrough = False
      .Superscript = False
      .Subscript = False
      .OutlineFont = False
      .Shadow = False
      .Underline = xlUnderlineStyleNone
      .Color = -11518420
      .TintAndShade = 0
      .ThemeFont = xlThemeFontNone
      End With
      End With
      xFileName = Dir
      Loop
      End If
      End Sub

      Please try it, hope it can help you!
  • To post as a guest, your comment is unpublished.
    Iulia Curtman · 6 months ago
    Very useful macro, and it works fine, but I would like to be able to select which files from that folder I want the macro to be ran on? The files are not generated automatically in a separate folder, and I need to run different macros on each set of files from that folder, and then move them back in the initial folder.