Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Open a sheet rename and move to another workbook

Ok,
I have gotten the code below to work BUT it loops on the same file instead
of moving on to the next file. My goal is to open all the files in a
particular directory, renmae the worksheet with the file name and then move
that worksheet to the workbook which the macro is running from i.e. in this
case book2.
Any fix for the incorrect looping?
Only learning here and so need a lot of help
Patrick


Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "F:\Work Stuff 2\Work Stuff\Promotion Report"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
Loop
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Open a sheet rename and move to another workbook

Patrick,
You need to get the next available file to work on:
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
TheFile =Dir() '<<<
Loop

Also you have the WB reference so why not use it to make it more clear which
workbook/sheet you are working on.
Also, how do you know what the ActiveSheet of the open workbook is ? It will
depend which was active when the workbook was last closed.
It would better to close the WB if you are finished to avoid having a lot of
open workbooks.

Do While TheFile < ""
Set WB = Workbooks.Open(MyPath & "\" & TheFile)
With WB.WorkSheets(1)
.Name = WB.Name
.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
End With
WB.Close True/False
TheFile =Dir() '<<<
Loop


"Patrick" wrote in message
...
Ok,
I have gotten the code below to work BUT it loops on the same file

instead
of moving on to the next file. My goal is to open all the files in a
particular directory, renmae the worksheet with the file name and then

move
that worksheet to the workbook which the macro is running from i.e. in

this
case book2.
Any fix for the incorrect looping?
Only learning here and so need a lot of help
Patrick


Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "F:\Work Stuff 2\Work Stuff\Promotion Report"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
Loop
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Open a sheet rename and move to another workbook

The Book2 reference is where the macro is running from and the book I want
the opened sheets to be moved to once they are renamed.
There is only one sheet in each workbook which is being opened and so once
that sheet is moved to the current book2 worksheet the original file would
normally close. I have done this already with a code which specifies file
names and sheet names BUT I now would like if I could make it more wild card
based due to the file names in the folder being changed and this would mean
changing the code each time (which might be every week) and any new files
might also get missed.
Patrick


"NickHK" wrote in message
...
Patrick,
You need to get the next available file to work on:
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
TheFile =Dir() '<<<
Loop

Also you have the WB reference so why not use it to make it more clear
which
workbook/sheet you are working on.
Also, how do you know what the ActiveSheet of the open workbook is ? It
will
depend which was active when the workbook was last closed.
It would better to close the WB if you are finished to avoid having a lot
of
open workbooks.

Do While TheFile < ""
Set WB = Workbooks.Open(MyPath & "\" & TheFile)
With WB.WorkSheets(1)
.Name = WB.Name
.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
End With
WB.Close True/False
TheFile =Dir() '<<<
Loop


"Patrick" wrote in message
...
Ok,
I have gotten the code below to work BUT it loops on the same file

instead
of moving on to the next file. My goal is to open all the files in a
particular directory, renmae the worksheet with the file name and then

move
that worksheet to the workbook which the macro is running from i.e. in

this
case book2.
Any fix for the incorrect looping?
Only learning here and so need a lot of help
Patrick


Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "F:\Work Stuff 2\Work Stuff\Promotion Report"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
Loop
End Sub






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Open a sheet rename and move to another workbook

Patrick,
You are already working on all .xls in that folder.
Or you mean you want to narrow the criteria of workbooks that are opened.
You can use a pattern in the initial Dir call e.g.
TheFile = Dir("Some Pattern*InFileName*.xls")

NickHK

"Patrick" wrote in message
...
The Book2 reference is where the macro is running from and the book I want
the opened sheets to be moved to once they are renamed.
There is only one sheet in each workbook which is being opened and so once
that sheet is moved to the current book2 worksheet the original file would
normally close. I have done this already with a code which specifies file
names and sheet names BUT I now would like if I could make it more wild

card
based due to the file names in the folder being changed and this would

mean
changing the code each time (which might be every week) and any new files
might also get missed.
Patrick


"NickHK" wrote in message
...
Patrick,
You need to get the next available file to work on:
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
TheFile =Dir() '<<<
Loop

Also you have the WB reference so why not use it to make it more clear
which
workbook/sheet you are working on.
Also, how do you know what the ActiveSheet of the open workbook is ? It
will
depend which was active when the workbook was last closed.
It would better to close the WB if you are finished to avoid having a

lot
of
open workbooks.

Do While TheFile < ""
Set WB = Workbooks.Open(MyPath & "\" & TheFile)
With WB.WorkSheets(1)
.Name = WB.Name
.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
End With
WB.Close True/False
TheFile =Dir() '<<<
Loop


"Patrick" wrote in message
...
Ok,
I have gotten the code below to work BUT it loops on the same file

instead
of moving on to the next file. My goal is to open all the files in a
particular directory, renmae the worksheet with the file name and then

move
that worksheet to the workbook which the macro is running from i.e. in

this
case book2.
Any fix for the incorrect looping?
Only learning here and so need a lot of help
Patrick


Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "F:\Work Stuff 2\Work Stuff\Promotion Report"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
Loop
End Sub








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Open a sheet rename and move to another workbook

Yes I know, but what is currently happening is that the macro loops on the
first file and keeps reopning it and moving it to the book2 and so it ends
up with endless sheets of the same workboo.
Patrick :)


"NickHK" wrote in message
...
Patrick,
You are already working on all .xls in that folder.
Or you mean you want to narrow the criteria of workbooks that are opened.
You can use a pattern in the initial Dir call e.g.
TheFile = Dir("Some Pattern*InFileName*.xls")

NickHK

"Patrick" wrote in message
...
The Book2 reference is where the macro is running from and the book I
want
the opened sheets to be moved to once they are renamed.
There is only one sheet in each workbook which is being opened and so
once
that sheet is moved to the current book2 worksheet the original file
would
normally close. I have done this already with a code which specifies file
names and sheet names BUT I now would like if I could make it more wild

card
based due to the file names in the folder being changed and this would

mean
changing the code each time (which might be every week) and any new files
might also get missed.
Patrick


"NickHK" wrote in message
...
Patrick,
You need to get the next available file to work on:
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
TheFile =Dir() '<<<
Loop

Also you have the WB reference so why not use it to make it more clear
which
workbook/sheet you are working on.
Also, how do you know what the ActiveSheet of the open workbook is ? It
will
depend which was active when the workbook was last closed.
It would better to close the WB if you are finished to avoid having a

lot
of
open workbooks.

Do While TheFile < ""
Set WB = Workbooks.Open(MyPath & "\" & TheFile)
With WB.WorkSheets(1)
.Name = WB.Name
.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
End With
WB.Close True/False
TheFile =Dir() '<<<
Loop


"Patrick" wrote in message
...
Ok,
I have gotten the code below to work BUT it loops on the same file
instead
of moving on to the next file. My goal is to open all the files in a
particular directory, renmae the worksheet with the file name and then
move
that worksheet to the workbook which the macro is running from i.e. in
this
case book2.
Any fix for the incorrect looping?
Only learning here and so need a lot of help
Patrick


Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "F:\Work Stuff 2\Work Stuff\Promotion Report"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
Loop
End Sub












  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Open a sheet rename and move to another workbook

So you did not add the extra I marked with '<<<<< in my previous post ?
Without that, yes you are going to reopen the same file. With that, you will
work through the files in the folder.

NickHK

"Patrick" wrote in message
...
Yes I know, but what is currently happening is that the macro loops on the
first file and keeps reopning it and moving it to the book2 and so it ends
up with endless sheets of the same workboo.
Patrick :)


"NickHK" wrote in message
...
Patrick,
You are already working on all .xls in that folder.
Or you mean you want to narrow the criteria of workbooks that are

opened.
You can use a pattern in the initial Dir call e.g.
TheFile = Dir("Some Pattern*InFileName*.xls")

NickHK

"Patrick" wrote in message
...
The Book2 reference is where the macro is running from and the book I
want
the opened sheets to be moved to once they are renamed.
There is only one sheet in each workbook which is being opened and so
once
that sheet is moved to the current book2 worksheet the original file
would
normally close. I have done this already with a code which specifies

file
names and sheet names BUT I now would like if I could make it more wild

card
based due to the file names in the folder being changed and this would

mean
changing the code each time (which might be every week) and any new

files
might also get missed.
Patrick


"NickHK" wrote in message
...
Patrick,
You need to get the next available file to work on:
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
TheFile =Dir() '<<<
Loop

Also you have the WB reference so why not use it to make it more

clear
which
workbook/sheet you are working on.
Also, how do you know what the ActiveSheet of the open workbook is ?

It
will
depend which was active when the workbook was last closed.
It would better to close the WB if you are finished to avoid having a

lot
of
open workbooks.

Do While TheFile < ""
Set WB = Workbooks.Open(MyPath & "\" & TheFile)
With WB.WorkSheets(1)
.Name = WB.Name
.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
End With
WB.Close True/False
TheFile =Dir() '<<<
Loop


"Patrick" wrote in message
...
Ok,
I have gotten the code below to work BUT it loops on the same file
instead
of moving on to the next file. My goal is to open all the files in a
particular directory, renmae the worksheet with the file name and

then
move
that worksheet to the workbook which the macro is running from i.e.

in
this
case book2.
Any fix for the incorrect looping?
Only learning here and so need a lot of help
Patrick


Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "F:\Work Stuff 2\Work Stuff\Promotion Report"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile < ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
ActiveSheet.Name = ActiveWorkbook.Name
ActiveSheet.Move After:=Workbooks("Book2").Sheets(Sheets.Count)
Loop
End Sub












Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Sheet-Rename feature is greyed-out in my Excel 2003 workbook.. Marlene Excel Discussion (Misc queries) 1 January 29th 10 09:43 PM
How to open a new workbook and rename the workbook? pokdbz Excel Discussion (Misc queries) 3 December 26th 07 03:37 PM
Move data to new sheet - rename sheet based on criteria ? [email protected] Excel Discussion (Misc queries) 7 May 16th 07 10:22 PM
Insert sheet, move to end, rename with cell data. [email protected] Excel Discussion (Misc queries) 2 October 11th 05 03:04 PM
Find and Open Workbook then copy and move sheet cwilson Excel Programming 0 February 2nd 05 07:29 PM


All times are GMT +1. The time now is 04:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"