Copy Data from 4th sheet in all the workbooks

Adnan11

New Member
Joined
Jul 1, 2019
Messages
5
Dear All,

I want to copy the data from sheet 4 named 'Unique MSISDNs' of all the workbooks in the location. Data to be copied is from A1 to H8. I am facing a problem that data overwrites previous one, data from only last file appears. I am using the below code, please can anyone help me in this.

Private Const sPath As String = "C:\Users\naveed\Downloads\Buzzme-Reports-2" 'CHANGE THIS TO YOUR DIRECTORY PATH

Sub LoopThroughFiles()


Dim sFile As String 'File Name
Dim sExt As String 'File extension you wish to open

sExt = "xlsx" 'Change this if extension is different

'loop through each file name and open it if the extension is correct
sFile = Dir(sPath)
Do Until sFile = ""
If Right(sFile, 4) = sExt Then GetInfo sFile
sFile = Dir
Loop




End Sub
Private Sub GetInfo(sFile As String)


Dim wbFrom As Workbook 'workbook to copy the data from
Dim iRow As Integer 'row number of next empty row




On Error GoTo errHandle

Application.EnableEvents = False
Application.ScreenUpdating = False

Set wbFrom = Workbooks.Open(sPath & sFile)


wbFrom.Sheets("Unique MSISDNs").Range("A1:H8").Copy 'Copy A1:H8
iRow = Me.Range("A" & Rows.Count).End(xlUp).Row + 1 'Get an empty row in this workbook
Me.Range("A1:H8" & iRow).PasteSpecial xlPasteAll 'past copied cells
wbFrom.Close (False)

Application.EnableEvents = True
Application.ScreenUpdating = True
Set wbFrom = Nothing

Exit Sub
errHandle:
MsgBox Err.Description
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Whats the name of the sheet where you want the copied cells pasted?
 
Upvote 0
Use this instead of the two lines you have:

Code:
With ThisWorkbook.Sheets("Sheet1")
    iRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    .Range("A" & iRow).PasteSpecial xlPasteAll
End With

Change sheet name to suit.
 
Upvote 0
Hi
Change the line
Me.Range("A1:H8" & iRow).PasteSpecial xlPasteAll 'past copied cells
to
Me.Range("A" & iRow).PasteSpecial xlPasteAll 'past copied cells
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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