Macro to copy selected cells from all the .xls files in a folder

rakeshplb

New Member
Joined
Apr 3, 2009
Messages
31
Hi All,

I have a folder "D:\Documents and Settings\Rakesh", which has many .xls files. Each file has a sheet called 'Cover Note'. I want to copy cells B2, C2, D4 and F3 from 'Cover Note' of each file.

These cells should be pasted in the current sheet, one row for each file. First cell of each row should have the source file name.

It would be better if macro can prompt to select the directory where ther source files resides.

Please, can anybody help me. Thanks.

Rakesh
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Code:
Sub test()
Dim wb As Workbook
Dim sht As Worksheet
Dim r As Integer
Set sht = ActiveSheet 'sheet for results
r = 2 '1st row
myDir = "D:\Documents and Settings\Rakesh\BS Recs\Feb 09\"
myfile = Dir(myDir & "*.xls")
Do While Len(myfile) > 0
Set wb = Workbooks.Open(myDir & myfile)
fnd = false
for each ws in wb.sheets
    if ws.name = "Cover Note" then fnd = true : exit for
next
if fnd then
With wb.Sheets("Cover Note")
sht.Cells(r, 1) = wb.Name
sht.Cells(r, 2) = .Range("B2")
sht.Cells(r, 3) = .Range("b2")
sht.Cells(r, 4) = .Range("D2")
sht.Cells(r, 5) = .Range("d5")
End With
else
msgbox "no cover note in " & wb.name
end if
wb.Close
myfile = Dir
r = r + 1
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,291
Messages
6,129,911
Members
449,540
Latest member
real_will_smith

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