+ Reply to Thread
Results 1 to 5 of 5

how to combine code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    how to combine code

    Hello. This first code opens all files in a folder and renames the first tab in each open file. The second adds the content from each tab into one tab. Is there a way that I can combine the two codes into one? So taht I don't have to run two macros?

    Code 1

    Sub RenameSheet1() 
    Dim FileExtension As String, FilesInFolder As String 
    Dim FolderPath As String 
    Dim wb As Workbook 
    
    'File Extension 'must contain wildcard "*" character 
    FileExtension = "*xls"
    
     'Folder Path 
    FolderPath = "Y:\Financial Analyst\Metrics\Claims\" 
    
    FilesInFolder = Dir(FolderPath & FileExtension) 
    
    Application.ScreenUpdating = False 
    
    'Loop through each file in folder 
    Do While FilesInFolder <> "" 
    'Set variable to opened workbook 
    Set wb = Workbooks.Open(Filename:=FolderPath & FilesInFolder) 
    
    'Change First sheet Name to File Name 
    wb.Worksheets(1).Name = Left(wb.Name, Len(wb.Name) - Len(FileExtension) - 1)
     'Close & Save 
    wb.Close True 
    'next file 
    FilesInFolder = Dir 
    Set wb = Nothing 
    Loop 
    Application.ScreenUpdating = True 
    End Sub
    Code 2

    Sub ammartino44()
    Dim wbk As Workbook
    Dim i As Long
    For Each wbk In Workbooks
    If wbk.Name <> "Master.xlsx" Then
    wbk.Sheets(1).UsedRange.Copy Workbooks("Master.xlsx").Sheets("Master").Range("A" & Rows.Count).End(3)(2)
    End If
    Next wbk
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: how to combine code

    Sub AM()
    RenameSheet1
    ammartino44
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: how to combine code

    Oh cool, but is my code inefficient? That is, would it be better to include the second piece of code as part of the first?

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: how to combine code

    It doesn't seem to be sloppy in any way!

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: how to combine code

    Thanks for the rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] If code same combine the description
    By Klitos in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-28-2013, 05:16 AM
  2. [SOLVED] Combine code in VBA
    By clifton1230 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2013, 12:21 AM
  3. Combine two vba code loops into one
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2011, 02:27 PM
  4. Combine & Shorten Code
    By QPM in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2010, 04:49 AM
  5. combine code
    By Dennisli2000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-12-2008, 12:17 AM

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