Results 1 to 2 of 2

Run macro for multiple excel files in a folder

Threaded View

ciprian Run macro for multiple excel... 04-29-2011, 02:39 AM
ciprian Re: Run macro for multiple... 04-30-2011, 05:12 PM
  1. #1
    Registered User
    Join Date
    04-18-2011
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2019
    Posts
    90

    Run macro for multiple excel files in a folder

    Hi guys, i have a macro that i need to run on many excel files. All of the files are located in the same folder.

    The macro, is a find/replace which uses data from a sheet ("sheet 2" in this case) and changes the data in "sheet 1".

    All of the excel files have only one sheet and the sheet name is the same which the nam,e of the file. For example:

    file name : 3 - 4
    sheet name : 3 - 4

    This is the macro that i use, big thanks to Leith Ross for writing it.


    Sub MultiReplace()
    
      Dim Cell As Range
      Dim Dict As Object
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Dict = CreateObject("Scripting.Dictionary")
        
          Set Wks = Worksheets("Sheet2")
        
          Set Rng = Wks.Range("A1")
          Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
          If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
          
            For Each Cell In Rng
              If Not IsEmpty(Cell) Then
                If Not Dict.Exists(Cell.Value) Then Dict.Add Cell.Value, Cell.Offset(0, 1).Value
              End If
            Next Cell
            
          Set Wks = Worksheets("Sheet1")
        
          Set Rng = Wks.Range("A1:B1") 'include 2 columns to be searched
          Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
          If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
            
            For Each Cell In Rng
              If Dict.Exists(Cell.Value) Then
                 Cell.Offset(0, 0) = Dict(Cell.Value) 'change value of the cell
              End If
            Next Cell
      
    End Sub
    Also the method should allow me to change which macro I want to run.

    Thanks a lot for the help
    Last edited by ciprian; 04-30-2011 at 05:13 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1