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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,419
Office Version
  1. 365
Platform
  1. Windows
Whats the name of the sheet where you want the copied cells pasted?
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,419
Office Version
  1. 365
Platform
  1. Windows
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.
 

Roger Govier

Active Member
Joined
Jun 19, 2002
Messages
303
Hi
Change the line
Me.Range("A1:H8" & iRow).PasteSpecial xlPasteAll 'past copied cells
to
Me.Range("A" & iRow).PasteSpecial xlPasteAll 'past copied cells
 

Watch MrExcel Video

Forum statistics

Threads
1,132,707
Messages
5,654,840
Members
418,156
Latest member
juliapearson

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
Top