Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:24:44+00:00 2026-06-09T03:24:44+00:00

I have many excel files in a folder. I wanted a macro to iterate

  • 0

I have many excel files in a folder.

I wanted a macro to iterate through each file and copy sheet named final cost and make a sheet with name of source file in destination file.

Like there are three files A, B, C each having a sheet named “final cost

The new file will have three sheets named

  • A,
  • B,
  • C

The edited code looks like

Sub RunCodeOnAllXLSFiles()
    Dim lCount As Long
    Dim wbResults As Workbook
    Dim wbCodeBook As Workbook

    'Application.ScreenUpdating = False
    'Application.DisplayAlerts = False
    'Application.EnableEvents = False

    'On Error Resume Next

    'Set wbCodeBook = ThisWorkbook

    Dim FilePath    As String, fName As String
    Dim aWB As Workbook, sWB As Workbook

    Set aWB = ActiveWorkbook
    FilePath = "D:\binny\" 'change to suit
    fName = Dir(FilePath & "*.xls")

    Do While fName <> ""
        If fName <> aWB.Name Then
            Set sWB = Workbooks.Open(FileName:=FilePath & fName, UpdateLinks:=0)
            sWB.Worksheets("Final Cost").Range("A1:Z6666").Copy
            sWB.Close False
            Sheets.Add.Name = fName
            Worksheets(fName).Range("D1").Select
            ActiveSheet.PasteSpecial Format:= _
            "Microsoft Word 8.0 Document Object"
        End If
        fName = Dir
    Loop
    Set sWB = Nothing: Set aWB = Nothing


               'Application.ScreenUpdating = True
    'Application.DisplayAlerts = True
    'Application.EnableEvents = True
End Sub

The things now to do are:

  1. preserve formatting and cell width
  2. I can’t get Paste Special to work
  3. Delete worksheet with same name if exists
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:24:47+00:00Added an answer on June 9, 2026 at 3:24 am

    You have got the most part figured out. Here is what I recommend.

    Set a name for 1 main worksheet in the file from where the macro is run so that you can delete all sheets except that one sheet in 1 go. Let’s say that the main sheet is “MainSheet”

    For example

    Sub Sample()
        Dim ws As Worksheet
    
        For Each ws In ThisWorkbook.Sheets
            If ws.Name <> "MainSheet" Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
            End If
        Next ws
    End Sub
    

    Now you can add this code to the beginning of your code. I have modified your code. All I am doing in your code is after the sheet is created, simply delete the columns after Z.

    See this (UNTESTED)

    Sub test()
        Dim FilePath As String, fName As String
        Dim aWB As Workbook, sWB As Workbook
        Dim ws As Worksheet
        Dim ColName As String
    
        Set aWB = ThisWorkbook
    
        '~~> Delete sheets
        For Each ws In aWB.Sheets
            If ws.Name <> "MainSheet" Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
            End If
        Next ws
    
        FilePath = "D:\binny\" '<~~ Change to suit
    
        fName = Dir(FilePath & "*.xls")
    
        Do While fName <> ""
            If fName <> aWB.Name Then
                Set sWB = Workbooks.Open(Filename:=FilePath & fName, UpdateLinks:=0)
                sWB.Sheets("Final Cost").Move after:=aWB.Sheets(aWB.Sheets.Count)
                sWB.Close False
                '~~> The sheet is copied, simply delete the columns after Z
                With aWB.Sheets(aWB.Sheets.Count)
                    .Name = fName
                    .Cells.Copy
                    .Cells.PasteSpecial xlPasteValues
                    '~~> Get the last column Name
                    ColName = Split(.Cells(, .Columns.Count).Address, "$")(1)
                    .Columns("AA:" & ColName).Delete
                End With
            End If
            fName = Dir
        Loop
        Set sWB = Nothing: Set aWB = Nothing
    End Sub
    

    Give it a try and if you get any errors, let me know which line and I will rectify it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two excel files. Among many columns in both these files, they have
I have many different NSArray 's stored in .dat files, in the Documents folder
I have many many files in a folder, and I want to process them
We have many projects with several files inside each. Files can be checked in
I have many text files containing bank transactions in the middle of them. Each
I have two excel files. First excel file contains the Person Name and Total
I have written an Excel VBA macro which imports data from a HTML file
I have got a folder with many excel documents in it on tomcat and
I have an Excel file (.xlsm) with a macro in it. I want to
I have a list of (many) employees in Excel/csv who take sick days, listed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.