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

  • Home
  • SEARCH
  • 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 8709545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:23:13+00:00 2026-06-13T04:23:13+00:00

the folowing code allows me to browse for multiple different excel files and paste

  • 0

the folowing code allows me to browse for multiple different excel files and paste them in a single sheet below each other.the excel file have the same column names but have different data in them and is working fine, my problem is i need it when it paste a file it must write the name of that file for each and every file it paste.The name of my excel file is called Familycar and the file name of other excel is called smartcar

example

eg1 CarName,Fuel,Colour

BMW,Petrol,Red

Ford,Diesel,Green

Mazda,Petrol,Grey

eg2 CarName,Fuel,Colour

Austin,Petrol,Blue

VW,Diesel,White

Audi,Petrol,Black

Result

CarName,Fuel,Colour,FileName

BMW,Petrol,Red,Familycar

Ford,Diesel,Green,Familycar

Mazda,Petrol,Grey,Familycar

Austin,Petrol,Blue,smatrtcar

VW,Diesel,White,smartcar

Audi,Petrol,Black,smartcar

   Sub Button5_Click()
 Dim fileStr As Variant
 Dim wbk1 As Workbook, wbk2 As Workbook
 Dim ws1 As Worksheet

 fileStr = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xlsx), *.xlsx", Title:="Get File", MultiSelect:=True)
 Set wbk1 = ActiveWorkbook
 Set ws1 = wbk1.Sheets("Sheet3")

 'handling first file seperately
 MsgBox fileStr(1), , GetFileName(CStr(fileStr(1)))
 Set wbk2 = Workbooks.Open(fileStr(1))
 wbk2.Sheets(1).UsedRange.Copy ws1.Cells(ws1.Range("A" & Rows.Count).End(xlUp).Row + 2, 1)

 wbk2.Close

 For i = 2 To UBound(fileStr)
 MsgBox fileStr(i), , GetFileName(CStr(fileStr(i)))

 Set wbk2 = Workbooks.Open(fileStr(i))

 wbk2.Sheets(1).UsedRange.Offset(1, 0).Copy ws1.Cells(ws1.Range("A" & Rows.Count).End(xlUp).Row + 2, 1)


 wbk2.Close
 Next i 
  • 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-13T04:23:14+00:00Added an answer on June 13, 2026 at 4:23 am

    Here’s your code refactored to include this requirement

    Sub Button5_Click()
        Dim fileStr As Variant
        Dim wbk1 As Workbook, wbk2 As Workbook
        Dim ws1 As Worksheet
        Dim rngSource As Range
        Dim rngDest As Range
        Dim rwOffset As Long
        Dim sFileName As String
    
        Dim i As Long
    
        fileStr = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xlsx), *.xlsx", Title:="Get File", MultiSelect:=True)
        Set wbk1 = ActiveWorkbook
        Set ws1 = wbk1.Sheets("Sheet3")
    
        For i = 1 To UBound(fileStr)
            MsgBox fileStr(i), , GetFileName(CStr(fileStr(i)))
    
            ' Used to change copy range for first file, without repeating code
            rwOffset = IIf(i = 1, 0, 1)
            Set wbk2 = Workbooks.Open(fileStr(i))
    
            ' File Name without extension
            sFileName = Left$(wbk2.Name, InStrRev(fileStr(i), ".") - 1)  
    
            Set rngSource = wbk2.Sheets(1).UsedRange.Offset(rwOffset, 0)
            Set rngDest = ws1.Cells(ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row + 2, 1)
    
            rngSource.Copy rngDest
    
            ' Add filename next to pasted data
            rngDest.Offset(0, rngSource.Columns.Count).Resize(rngSource.Rows.Count, 1) = sFileName
            wbk2.Close
        Next i
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the folowing code allows me to browse for multiple different excel files and paste
Using the jQuery Cycle plugin, the following code allows user to browse the photos/:
The relevant solution and project files for this question are at: http://code.google.com/p/benfwaves/source/browse/trunk/BenfWaves.sln?r=26 http://code.google.com/p/benfwaves/source/browse/trunk/BenfWaves.Library/BenfWaves.Library.csproj?r=26 http://code.google.com/p/benfwaves/source/browse/trunk/BenfWaves.Client/BenfWaves.Client.csproj?r=26
The following code uploads multiple files from a browser in a serial fashion to
The following code allows me to toggle a div inside an LI, how can
I have the following code that allows my console app to go to the
The following code shows a button that allows you to select a file (should
I was wondering how can I allow NULL values in the following code below
I have folowing code in which i am using some conditions on page to
How would you make the folowing code functional with the same speed? In general,

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.