How to copy paste data from multiple excel sheet in one workbook in sequence?

Dave Smith

New Member
Joined
Jul 5, 2021
Messages
32
Office Version
  1. 2016
Platform
  1. Windows
Hi,

A big thanks in advance,

I want to create a database using VBA code where I need to copy certain data from multiple excel file in to one excel file.
The user will enter the path of the folder where the multiple excel files are kept, and the code should copy following thing's from multiple excel in to one excel file:
1. file name
2. customer name
3. location
4. product name
5. date

I want to do this process in sequence like 1st enter the file name which is opened in master file, then copy specific cell value from the opened excel file & activate the master file and paste it.
2nd close the opened file open second file and repeat the copy paste procedure.
3rd after copying all the data from all the files present in the folder it should save the master database excel file


I have written the code to get file name from the folder in master data base file as shown below but i am not able to figuring out how to open that file copy and paste in this in sequence

Sub getfolderdfilenames()


Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objfile As Scripting.File
Dim nextrow As Long

Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\SHMEHTA\Desktop\CFD Tool For Request")

nextrow = Database.Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objfile In objFolder.Files

Database.Cells(nextrow, 2) = objfile.Name
nextrow = nextrow + 1

Next objfile
End Sub

Any Help is appreciated plz....

Regards,
Dave
 

Attachments

  • master database image.png
    master database image.png
    44.4 KB · Views: 85
  • multiple excel sheet value.png
    multiple excel sheet value.png
    19.9 KB · Views: 68
If the names are on the sheet, then you need a different macro, not like the one you put in the OP.
You should not read all the files in the folder, only the ones that interest you.

Try this:
VBA Code:
Sub GetInfoFromFiles()
  Dim wb As Workbook, p As String, c As Range
  p = "C:\Users\SHMEHTA\Desktop\CFD Tool For Request\"
  Application.ScreenUpdating = False
  For Each c In Database.Range("B3", Database.Range("B" & Rows.Count).End(3))
    If Dir(p & c.Value) <> "" Then
      Set wb = Workbooks.Open(p & c.Value)
      Database.Cells(c.Row, 3).Resize(1, 4).Value = Array(wb.Sheets(1).[b1], wb.Sheets(1).[b2], wb.Sheets(1).[B6], wb.Sheets(1).[B7])
      wb.Close False
    End If
  Next
  ActiveWorkbook.Save
End Sub
Sorry DanteAmor for giving you little bit more trouble.... i am extremely sorry

I have tried this code but its not working for me like its not copying data from the other files... :(
The 1st code which you shared works so good that it does everything which i need to do by using macro only problem in that code is taking data from all the file present in the folder every time i run the macro instead of taking data only from the new file.
Can you pl help me in that pl..... i request you....

Regards,
Dave
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You must put the complete example.
What do you have in the "Database" sheet, in column "B". Before running the macro.
Now each of those files in column B, what do they have on the first sheet, you must put the data of each file here. Take for example 2 or 3 files. Use XL2BB tool minisheets.
And lastly, and very importantly, what is the result in the database sheet.

My examples:
Sheet Database berfore running the macro:
varios 19ene2022.xlsm
ABCDEF
1Database
2No.File nameDateLocationCustomerProduct
31Audit.xlsx
42Copy of Template 2.xlsm
53destination.xlsx
Hoja1


workbook audit:
Audit.xlsx
AB
1Date15/03/2022
2locationcdmx
3iteration1
4
5
6customeralc001
7producttopbox
Sheet1


workbook Copy of Template 2
Copy of Template 2.xlsx
AB
1Date22/07/2022
2locationedomex
3iteration2
4
5
6customernextel
7productmodem-we
Sheet1


workbook destination:
destination.xlsx
AB
1Date02/01/2022
2locationgdl
3iteration3
4
5
6customermorena
7productsmarttv
Sheet1


Result after running the macro
varios 19ene2022.xlsm
ABCDEF
1Database
2No.File nameDateLocationCustomerProduct
31Audit.xlsx15/03/2022cdmxalc001topbox
42Copy of Template 2.xlsx22/07/2022edomexnextelmodem-we
53destination.xlsx02/01/2022gdlmorenasmarttv
Hoja1


If you contribute with good examples, it will be easier to help you.
 
Upvote 0
You must put the complete example.
What do you have in the "Database" sheet, in column "B". Before running the macro.
Now each of those files in column B, what do they have on the first sheet, you must put the data of each file here. Take for example 2 or 3 files. Use XL2BB tool minisheets.
And lastly, and very importantly, what is the result in the database sheet.

My examples:
Sheet Database berfore running the macro:
varios 19ene2022.xlsm
ABCDEF
1Database
2No.File nameDateLocationCustomerProduct
31Audit.xlsx
42Copy of Template 2.xlsm
53destination.xlsx
Hoja1


workbook audit:
Audit.xlsx
AB
1Date15/03/2022
2locationcdmx
3iteration1
4
5
6customeralc001
7producttopbox
Sheet1


workbook Copy of Template 2
Copy of Template 2.xlsx
AB
1Date22/07/2022
2locationedomex
3iteration2
4
5
6customernextel
7productmodem-we
Sheet1


workbook destination:
destination.xlsx
AB
1Date02/01/2022
2locationgdl
3iteration3
4
5
6customermorena
7productsmarttv
Sheet1


Result after running the macro
varios 19ene2022.xlsm
ABCDEF
1Database
2No.File nameDateLocationCustomerProduct
31Audit.xlsx15/03/2022cdmxalc001topbox
42Copy of Template 2.xlsx22/07/2022edomexnextelmodem-we
53destination.xlsx02/01/2022gdlmorenasmarttv
Hoja1


If you contribute with good examples, it will be easier to help you.
 
Upvote 0
You must put the complete example.
What do you have in the "Database" sheet, in column "B". Before running the macro.
Now each of those files in column B, what do they have on the first sheet, you must put the data of each file here. Take for example 2 or 3 files. Use XL2BB tool minisheets.
And lastly, and very importantly, what is the result in the database sheet.

My examples:
Sheet Database berfore running the macro:
varios 19ene2022.xlsm
ABCDEF
1Database
2No.File nameDateLocationCustomerProduct
31Audit.xlsx
42Copy of Template 2.xlsm
53destination.xlsx
Hoja1


workbook audit:
Audit.xlsx
AB
1Date15/03/2022
2locationcdmx
3iteration1
4
5
6customeralc001
7producttopbox
Sheet1


workbook Copy of Template 2
Copy of Template 2.xlsx
AB
1Date22/07/2022
2locationedomex
3iteration2
4
5
6customernextel
7productmodem-we
Sheet1


workbook destination:
destination.xlsx
AB
1Date02/01/2022
2locationgdl
3iteration3
4
5
6customermorena
7productsmarttv
Sheet1


Result after running the macro
varios 19ene2022.xlsm
ABCDEF
1Database
2No.File nameDateLocationCustomerProduct
31Audit.xlsx15/03/2022cdmxalc001topbox
42Copy of Template 2.xlsx22/07/2022edomexnextelmodem-we
53destination.xlsx02/01/2022gdlmorenasmarttv
Hoja1


If you contribute with good examples, it will be easier to help you.
OK DanteAmor I got your question & sorry for not expalining how the files looks before and after running the macro

There will be a folder (named Database) where all the excel files will be kept and with in that there is a folder (Database file) where only master database file is kept. Now i will keep new files in folder named Database.
The macro should compare the file name located in the master data base file in "column B" with the files present in the Database folder, now if the file name "File_1.xlsx" is present in the master database then the macro should no copy data again from it rather then it should go to other file present in the folder to get file name and other info.

Eg: Now if I put the new file name "File_2.xlsx" in the database folder then the macro should get the data only from this file only instead of getting data again from the file_1

Before
master database.xlsm
ABCDEF
1Database
2Sr No.File nameDateLocationCustomer name Product name
31File_1.xlsx18/1/2022GHFHTUSpanner(M16)
4
5
6
7
8
database




After running macro

master database.xlsm
ABCDEF
1Database
2Sr No.File nameDateLocationCustomer name Product name
31File_1.xlsx18/1/2022GHFHTUSpanner(M16)
42File_2.xlsx16/1/2022EVGIBSWasher(M45)
5
6
database



Also pl. have a look to the images Which I am attaching to get an idea of the folder, files name and location.

I hope i have tried to explain properly....
 

Attachments

  • 9658.JPG
    9658.JPG
    15 KB · Views: 19
  • 9865.JPG
    9865.JPG
    42.7 KB · Views: 18
Upvote 0
It would be fabulous if your example is complete, XL2BB minisheet is missing from File_2.xlsx file. We practically have to guess where the data goes.
The information is obvious to you because you are familiar with your data.
Remember, you are asking us for help, you must provide complete information.
I don't understand where you get the value of column A "Sr No" is it a consecutive or you get it from the file?
-----
Also pl. have a look to the images Which I am attaching to get an idea of the folder, files name and location.
I don't understand what you mean by that.
The folder is written in the macro code, just as you put it in your OP:
Set objFolder = objFSO.GetFolder("C:\Users\SHMEHTA\Desktop\CFD Tool For Request")
-----
Try this:

VBA Code:
Sub getfolderdfilenames_3()
  Dim objFSO As Scripting.FileSystemObject
  Dim objFolder As Scripting.Folder
  Dim objfile As Scripting.File
  Dim nextrow As Long
  Dim wb As Workbook
  Dim f As Range
  Dim sName As String
  
  Application.ScreenUpdating = False
  Set objFSO = CreateObject("Scripting.FilesystemObject")
  Set objFolder = objFSO.GetFolder("C:\Users\SHMEHTA\Desktop\CFD Tool For Request")
  
  nextrow = Database.Cells(Rows.Count, 1).End(xlUp).Row + 1
  For Each objfile In objFolder.Files
    If InStrRev(objfile, ".xls", , vbTextCompare) > 0 Then
      sName = Mid(objfile, InStrRev(objfile, "\") + 1)
      Set f = Database.Range("B:B").Find(sName, , xlValues, xlWhole, , , False)
      If f Is Nothing Then
        Set wb = Workbooks.Open(objfile)
        Database.Cells(nextrow, 1) = wb.Sheets(1).Range("B3")
        Database.Cells(nextrow, 2) = objfile.Name
        Database.Cells(nextrow, 3) = wb.Sheets(1).Range("B1")
        Database.Cells(nextrow, 4) = wb.Sheets(1).Range("B2")
        Database.Cells(nextrow, 5) = wb.Sheets(1).Range("B6")
        Database.Cells(nextrow, 6) = wb.Sheets(1).Range("B7")
        nextrow = nextrow + 1
        wb.Close False
      End If
    End If
  Next objfile
  ActiveWorkbook.Save
End Sub
 
Upvote 0
Solution
It would be fabulous if your example is complete, XL2BB minisheet is missing from File_2.xlsx file. We practically have to guess where the data goes.
The information is obvious to you because you are familiar with your data.
Remember, you are asking us for help, you must provide complete information.
I don't understand where you get the value of column A "Sr No" is it a consecutive or you get it from the file?
-----

I don't understand what you mean by that.
The folder is written in the macro code, just as you put it in your OP:

-----
Try this:

VBA Code:
Sub getfolderdfilenames_3()
  Dim objFSO As Scripting.FileSystemObject
  Dim objFolder As Scripting.Folder
  Dim objfile As Scripting.File
  Dim nextrow As Long
  Dim wb As Workbook
  Dim f As Range
  Dim sName As String
 
  Application.ScreenUpdating = False
  Set objFSO = CreateObject("Scripting.FilesystemObject")
  Set objFolder = objFSO.GetFolder("C:\Users\SHMEHTA\Desktop\CFD Tool For Request")
 
  nextrow = Database.Cells(Rows.Count, 1).End(xlUp).Row + 1
  For Each objfile In objFolder.Files
    If InStrRev(objfile, ".xls", , vbTextCompare) > 0 Then
      sName = Mid(objfile, InStrRev(objfile, "\") + 1)
      Set f = Database.Range("B:B").Find(sName, , xlValues, xlWhole, , , False)
      If f Is Nothing Then
        Set wb = Workbooks.Open(objfile)
        Database.Cells(nextrow, 1) = wb.Sheets(1).Range("B3")
        Database.Cells(nextrow, 2) = objfile.Name
        Database.Cells(nextrow, 3) = wb.Sheets(1).Range("B1")
        Database.Cells(nextrow, 4) = wb.Sheets(1).Range("B2")
        Database.Cells(nextrow, 5) = wb.Sheets(1).Range("B6")
        Database.Cells(nextrow, 6) = wb.Sheets(1).Range("B7")
        nextrow = nextrow + 1
        wb.Close False
      End If
    End If
  Next objfile
  ActiveWorkbook.Save
End Sub
A big thank you DanteAmor For this fabulous code It completely worked for me :)
Once again thank you for helping me :)
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top