Copy Data to Master Database

sureshtrb

Board Regular
Joined
Mar 24, 2013
Messages
106
Tried the below code with minor modification to select file referred from
Open 132 files and copy data into master file

My problem is its not copying the entire data. Copies only few 3 or 4 rows.
Please help

Code:
Option Explicit

Public Sub CommandButton2_Click()
Dim Master As Workbook
Dim sourceBook As Workbook
Dim sourceData As Worksheet
Dim CurrentFileName As String
Dim myPath As String
Dim MyFile As Object
Dim Fileselected As String


Application.ScreenUpdating = False
Application.DisplayAlerts = False


'The folder containing the files to be recap'd
myPath = "C:\Users\VSD\Desktop\Reports\"






Set MyFile = Application.FileDialog(msoFileDialogFilePicker)
With MyFile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
Fileselected = .SelectedItems(1)
End With


'Finds the name of the first file of type .xls in the current directory
CurrentFileName = Dir(Fileselected)


'Create a workbook for the recap report
Set Master = ThisWorkbook


Do
    Workbooks.Open (myPath & CurrentFileName)
    Set sourceBook = Workbooks(CurrentFileName)
    Set sourceData = sourceBook.Worksheets(2)
    
        With sourceData
           .Range("A5:FT" & Range("A" & Rows.Count).End(xlUp).Row).Copy Master.Worksheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        End With
       
    sourceBook.Close
  
'Calling DIR w/o argument finds the next .xlsx file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""


MsgBox "Data Copied to Master DataBase-" & vbNewLine & "Done"


Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub
 
Re: [Solved]Re: Copy Data to Master Database

Tried not successful

sorry, should read what I type before I post!

see if this helps:

Code:
Dim dest As Range
Do
    Set sourcebook = Workbooks.Open(myPath & CurrentFileName)
    Set sourcedata = sourcebook.Worksheets(2)
    
    With master.Worksheets(2)
    Set dest = .Cells((.Range("A" & .Rows.Count).End(xlUp).Row), 1)
    End With
        
        dest.Offset(1, 0).Value = Format(Date, "dd/mm/yyyy")
    
        With sourcedata
           .Range("A5:FT" & .UsedRange.Rows.Count).Copy dest.Offset(2, 0)
        End With
       
    sourcebook.Close False
    Set dest = Nothing
  
'Calling DIR w/o argument finds the next .xlsx file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""

Dave
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Re: [Solved]Re: Copy Data to Master Database

You Are Great.
Yes Got What I want.
Thanks for the effort and time spent.
 
Upvote 0
Re: [Solved]Re: Copy Data to Master Database

Dear Dave,
minor change.
when I use the given code, during retrieval, the copied rows were not reflected on the userform and changed to my requirement.
Now working perfectly.
Just for your information and for somebody who may in need.
Thanks
Below the working code
Code:
Public Sub CommandButton2_Click()Dim Master As Workbook
Dim sourceBook As Workbook
Dim sourceData As Worksheet
Dim CurrentFileName As String
Dim myPath As String
Dim myFile As Object
Dim Fileselected As String


Application.ScreenUpdating = False
Application.DisplayAlerts = False


'The folder containing the files to be recap'd
myPath = "C:\Users\xxx\Desktop\xxxx\"






Set myFile = Application.FileDialog(msoFileDialogFilePicker)
With myFile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
Fileselected = .SelectedItems(1)
End With


'Finds the name of the first file of type .xls in the current directory
CurrentFileName = Dir(Fileselected)


'Create a workbook for the recap report
Set Master = ThisWorkbook
  
       
    Dim dest As Range
Do
    Set sourceBook = Workbooks.Open(myPath & CurrentFileName)
    Set sourceData = sourceBook.Worksheets(2)
    
    With Master.Worksheets(2)
    Set dest = .Cells((.Range("A" & .Rows.Count).End(xlUp).Row), 1)
    End With
        
        dest.Offset(0, 0).Value = Format(Date, "mmmm/dd/yyyy")
    
        With sourceData
           .Range("A5:FT" & .UsedRange.Rows.Count).Copy dest.Offset(1, 0)
        End With
       
    sourceBook.Close True
    Set dest = Nothing
  
'Calling DIR w/o argument finds the next .xlsx file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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