question about loop to copy content in folder to a worksheet

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
Hi,

I have a bunch of .xls files in a folder on my desktop (let's call it myFolder).

If I want to copy the contents of worksheet called "Template" in every .xls file in that folder (except for the workbook I am running the macro from) , any suggestions on how to do this? I am creating a macro in the Master file to run. I am concatenating the contents to the "ALLData" worksheet in the active workbook.

-Eileen


Set wbk = ThisWorkbook
Set sht = wbk.Sheets("ALLData")

Generate an array of file names containing .xls files from the folder
Delete the file name from this list where the file name matches the current active workbook

CurrentIndex = 0

' last index is the number of entries in the array
do until CurreintIndex < LastIndex
Workbooks.Open file name in array[current index]

' select rows 2 through the last row of the worksheet to be copied
' copy those rows
' paste those rows after the last row of sht (target sheet)
loop
 
I thought that might come up.:)

Again untested.
Code:
Set rngCopy = wsSrc.Range("A1").CurrentRegion

Set rngCopy = rngCopy.Offset(1).Resize(rngCopy.Rows.Count-1)
rngCopy.Copy wsDst.Range("A" & NextRow)
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Also, how do I specify the logic to find the current directory?

strDirName = CurDir()

does not give the directory of the workbook I am executing the script from.
 
Upvote 0
Wouldn't it be easier to retain the below logic

wsSrc.Range("A1").CurrentRegion.Copy wsDst.Range("A" & NextRow)

but delete the contents of NextRow (which would be the first row of the sheet being copied which I want to throw away)?
 
Upvote 0
In the code I posted I simply specified the directory here.
Code:
strDirName = "C:\myFolder\"
Which was the directory you mentioned in the original post.

As to the other question, yes you might be able to do that but it's probably safer to not copy the 1st row in the first place.
 
Upvote 0
If I add this to the end of the subroutine, why do I have a problem?

wsDst.Select
With Selection
.WrapText = False
End With

The logic at the beginning of the routine was this:

Set wbDst = ThisWorkbook
Set wsDst = wbDst.Sheets("AllData")

It seems to have a problem with the "wsDst.Select" line.

I am trying to disable wraparound for the AllData sheet after the copying operation is done.
 
Upvote 0
Eh, I far as I recall I didn't post any code like this.:)
Code:
wsDst.Select
 
Upvote 0
comment about extra code at the end

Hi,

I tried to add some extra at the code to disable word wrapping in the AllData worksheet. This is not your code.

-Eileen
 
Upvote 0
Here is what I have now. the code after "End With" is not working. I am trying to select the "AllData" sheet and disable word wrap. Any advice?

Code:
    Set wbDst = ThisWorkbook
    Set wsDst = wbDst.Sheets("AllData")
    Set wsLog = wbDst.Sheets("Log")
    Set oSearch = Application.FileSearch

    wsLog.Cells.Clear
    wsDst.Cells.Clear
    
    With oSearch
           .
           .
           .
    End With

    wsDst.Select
    Cells.Select
    With Selection
        .WrapText = False
    End With
[/code]
 
Upvote 0
Try this.
Code:
wsDst.Cells.WrapText = False
 
Upvote 0
question about executing script from web posting

Hi,

Is it possible to execute an excel macro from an excel file posted on a web page? If I do this, what kind of path would I have to specify in place of

strDirName = "C:\myFolder\"

Let's say I am executing the script from an opened up file at http://<my url>. The macro was expecting the files to be copied to be located in a folder at strDirName. I don't know if I can avoid copying the files posted on the web to a local folder.

-Eileen
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,772
Members
449,336
Latest member
p17tootie

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