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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi sure,

Your code is copying only columns A:FT and only the range from A5 down to the last populated cell in Column A.

If Column A is not populated on the last line of your code, you will be missing lines.

Can you check to see if this is the problem first? Then we can look at other things.
 
Upvote 0
The data starts from A5 only. Upto A4 are headings, reference etc.,
Column A is the Report number and without that, report will not be generated during data entry thro userform.
All the rows have A column filled in the data sheet to be copied.
 
Upvote 0
the ranges you are copying are not qualified to the sourcedata worksheet.

see if changes shown in RED help:

Rich (BB code):
With sourceData
           .Range("A5:FT" & .Range("A" & .Rows.Count).End(xlUp).Row).Copy Master.Worksheets(2).Range("A" & Master.Worksheets(2).Rows.Count).End(xlUp).Offset(1, 0)
        End With

Dave
 
Upvote 0
Thanks for your suggestion and tried your method: result below:
first time when i press button and it copied 1 row only
second time when i press to copy from same file, it copied 2 row ie. row 1(same row) and row 2
3rd time 4 rows
4th time 8 rows
5th time 16 rows
and
6th time from different file i tried and still copied 36 rows!
eccentric?
 
Upvote 0
try using UsedRange & see if that helps:

Code:
With sourceData
           .Range("A5:FT" & .UsedRange.Rows.Count).Copy Master.Worksheets(2).Range("A" & Master.Worksheets(2).Rows.Count).End(xlUp).Offset(1, 0)
        End With

Dave
 
Upvote 0
[Solved]Re: Copy Data to Master Database

Excellent worked!. Thanks...
further, Will it be possible to create a date on the first row of the copied data to segregate the details between the data?
 
Last edited:
Upvote 0
Re: [Solved]Re: Copy Data to Master Database

Excellent worked!. Thanks...
further, Will it be possible to create a date on the first row of the copied data to segregate the details between the data?

you can try this as an idea:

Code:
Dim Dest As Range
Do
    Set sourceBook = Workbooks.Open(myPath & CurrentFileName)
    Set sourceData = sourceBook.Worksheets(2)
    
    With Master.Worksheets(2)
    Set Dest = .Range("A" & .Rows.Count).End(xlUp).Row
    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
  
'Calling DIR w/o argument finds the next .xlsx file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""

Dave
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
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