PC Review


Reply
Thread Tools Rate Thread

Change My Macro to Prompt User to Select Folder

 
 
bac
Guest
Posts: n/a
 
      13th Nov 2006
Presently it opens a specific folder, and pulls sheet 9 of all
spreadsheets into a new spreadsheet called Master 1. I would like for
the user to select the folder, rather than having a specific path
identified in the macro.

This is the Macro I would like to change.
Sub Combine()


Fpath = "C:\home\xxxxxxxxxxxxx\xxxxxxxx\4010\" ' change to suit
your directory
fName = Dir(Fpath & "*.xls")

Do While fName <> ""
Workbooks.Open Fpath & fName
Sheets(9).Copy
after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count)
Workbooks(fName).Close savechanges:=False
fName = Dir
Loop

End Sub


I'll really appreciate any help with problem!
thanks

 
Reply With Quote
 
 
 
 
Mark Ivey
Guest
Posts: n/a
 
      13th Nov 2006
Give this a go...

Sub Combine()
Dim Fpath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = False Then Exit Sub
Fpath = .SelectedItems(1)
End With

fName = Dir(Fpath & "*.xls")

Do While fName <> ""
Workbooks.Open Fpath & fName
Sheets(9).Copy
after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count) Workbooks(fName).Close savechanges:=False fName = Dir Loop End Sub"bac" <(E-Mail Removed)> wrote in messagenews:(E-Mail Removed)...> Presently it opens a specific folder, and pulls sheet 9 of all> spreadsheets into a new spreadsheet called Master 1. I would like for> the user to select the folder, rather than having a specific path> identified in the macro.>> This is the Macro I would like to change.> Sub Combine()>>> Fpath = "C:\home\xxxxxxxxxxxxx\xxxxxxxx\4010\" ' change to suit> your directory> fName = Dir(Fpath & "*.xls")>> Do While fName <> ""> Workbooks.Open Fpath & fName> Sheets(9).Copy>after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count)> Workbooks(fName).Close savechanges:=False> fName = Dir> Loop>> End Sub>>> I'll really appreciate any help with problem!> thanks>

 
Reply With Quote
 
Mark Ivey
Guest
Posts: n/a
 
      13th Nov 2006
Some of the wrong text got connected with the paste...

Give this one a try...


Sub Combine()
Dim Fpath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = False Then Exit Sub
Fpath = .SelectedItems(1)
End With
fName = Dir(Fpath & "*.xls")

Do While fName <> ""
Workbooks.Open Fpath & fName
Sheets(9).Copy
after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count)
Workbooks(fName).Close savechanges:=False
fName = Dir
Loop

End Sub


 
Reply With Quote
 
bac
Guest
Posts: n/a
 
      13th Nov 2006
Mark,

Thanks for getting me started. I've tried your suggestion.
I'm being prompted to select folder, but it does not proceed to copy
sheet(9) from all spreadsheets.
Any idea why?
Thanks, BAC

Mark Ivey wrote:
> Give this a go...
>
> Sub Combine()
> Dim Fpath As String
> With Application.FileDialog(msoFileDialogFolderPicker)
> .AllowMultiSelect = False
> If .Show = False Then Exit Sub
> Fpath = .SelectedItems(1)
> End With
>
> fName = Dir(Fpath & "*.xls")
>
> Do While fName <> ""
> Workbooks.Open Fpath & fName
> Sheets(9).Copy
> after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count) Workbooks(fName).Close savechanges:=False fName = Dir Loop End Sub"bac" <(E-Mail Removed)> wrote in messagenews:(E-Mail Removed)...> Presently it opens a specific folder, and pulls sheet 9 of all> spreadsheets into a new spreadsheet called Master 1. I would like for> the user to select the folder, rather than having a specific path> identified in the macro.>> This is the Macro I would like to change.> Sub Combine()>>> Fpath = "C:\home\xxxxxxxxxxxxx\xxxxxxxx\4010\" ' change to suit> your directory> fName = Dir(Fpath & "*.xls")>> Do While fName <> ""> Workbooks.Open Fpath & fName> Sheets(9).Copy>after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count)> Workbooks(fName).Close savechanges:=False> fName = Dir> Loop>> End Sub>>> I'll really appreciate any help with problem!> thanks>


 
Reply With Quote
 
bac
Guest
Posts: n/a
 
      13th Nov 2006
Mark,
Still having challenges! Any other ideas?
Thanks
bac
Mark Ivey wrote:
> Some of the wrong text got connected with the paste...
>
> Give this one a try...
>
>
> Sub Combine()
> Dim Fpath As String
> With Application.FileDialog(msoFileDialogFolderPicker)
> .AllowMultiSelect = False
> If .Show = False Then Exit Sub
> Fpath = .SelectedItems(1)
> End With
> fName = Dir(Fpath & "*.xls")
>
> Do While fName <> ""
> Workbooks.Open Fpath & fName
> Sheets(9).Copy
> after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count)
> Workbooks(fName).Close savechanges:=False
> fName = Dir
> Loop
>
> End Sub


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      13th Nov 2006
Sub Combine()
Dim Fpath As String
Dim Fname As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = False Then Exit Sub
Fpath = .SelectedItems(1)
End With
Fname = Dir(Fpath & "\*.xls")

Do While Fname <> ""
Workbooks.Open Fpath & Fname
Sheets(9).Copy after:=Workbooks("Master1.xls") _
.Sheets(Workbooks("Master1.xls").Sheets.Count)
Workbooks(Fname).Close savechanges:=False
Fname = Dir
Loop

End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"bac" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mark,
> Still having challenges! Any other ideas?
> Thanks
> bac
> Mark Ivey wrote:
> > Some of the wrong text got connected with the paste...
> >
> > Give this one a try...
> >
> >
> > Sub Combine()
> > Dim Fpath As String
> > With Application.FileDialog(msoFileDialogFolderPicker)
> > .AllowMultiSelect = False
> > If .Show = False Then Exit Sub
> > Fpath = .SelectedItems(1)
> > End With
> > fName = Dir(Fpath & "*.xls")
> >
> > Do While fName <> ""
> > Workbooks.Open Fpath & fName
> > Sheets(9).Copy
> >

after:=Workbooks("Master1.xls").Sheets(Workbooks("Master1.xls").Sheets.Count
)
> > Workbooks(fName).Close savechanges:=False
> > fName = Dir
> > Loop
> >
> > End Sub

>



 
Reply With Quote
 
Chris Lewis
Guest
Posts: n/a
 
      13th Nov 2006

"Bob Phillips" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Sub Combine()
> Dim Fpath As String
> Dim Fname As String
> With Application.FileDialog(msoFileDialogFolderPicker)
> .AllowMultiSelect = False
> If .Show = False Then Exit Sub
> Fpath = .SelectedItems(1)
> End With
> Fname = Dir(Fpath & "\*.xls")
>
> Do While Fname <> ""
> Workbooks.Open Fpath & Fname
> Sheets(9).Copy after:=Workbooks("Master1.xls") _
> .Sheets(Workbooks("Master1.xls").Sheets.Count)
> Workbooks(Fname).Close savechanges:=False
> Fname = Dir
> Loop
>
> End Sub
>
>
> --
> HTH
>
> Bob Phillips
>
> (replace somewhere in email address with gmail if mailing direct)



It is because you add the slash in the Fname = Dir(Fpath & "\*.xls") line
but dont add the required slash in the Workbooks.Open line. Change the
Woorkbooks.Open as below and it should work.


Sub Combine()
Dim Fpath As String
Dim Fname As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = False Then Exit Sub
Fpath = .SelectedItems(1)
End With
Fname = Dir(Fpath & "\*.xls")

Do While Fname <> ""
Workbooks.Open Fpath & "\" & Fname
Sheets(9).Copy after:=Workbooks("Master1.xls") _
.Sheets(Workbooks("Master1.xls").Sheets.Count)
Workbooks(Fname).Close savechanges:=False
Fname = Dir
Loop

End Sub


--
Chris Lewis


 
Reply With Quote
 
bac
Guest
Posts: n/a
 
      13th Nov 2006
Chris, Bob & Mark,

You are the Bomb! Thanks Chris for noticing the missing \. It works
GREAT! Just what I was looking for.

THANK YOU GUYS!

Chris Lewis wrote:
> "Bob Phillips" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Sub Combine()
> > Dim Fpath As String
> > Dim Fname As String
> > With Application.FileDialog(msoFileDialogFolderPicker)
> > .AllowMultiSelect = False
> > If .Show = False Then Exit Sub
> > Fpath = .SelectedItems(1)
> > End With
> > Fname = Dir(Fpath & "\*.xls")
> >
> > Do While Fname <> ""
> > Workbooks.Open Fpath & Fname
> > Sheets(9).Copy after:=Workbooks("Master1.xls") _
> > .Sheets(Workbooks("Master1.xls").Sheets.Count)
> > Workbooks(Fname).Close savechanges:=False
> > Fname = Dir
> > Loop
> >
> > End Sub
> >
> >
> > --
> > HTH
> >
> > Bob Phillips
> >
> > (replace somewhere in email address with gmail if mailing direct)

>
>
> It is because you add the slash in the Fname = Dir(Fpath & "\*.xls") line
> but dont add the required slash in the Workbooks.Open line. Change the
> Woorkbooks.Open as below and it should work.
>
>
> Sub Combine()
> Dim Fpath As String
> Dim Fname As String
> With Application.FileDialog(msoFileDialogFolderPicker)
> .AllowMultiSelect = False
> If .Show = False Then Exit Sub
> Fpath = .SelectedItems(1)
> End With
> Fname = Dir(Fpath & "\*.xls")
>
> Do While Fname <> ""
> Workbooks.Open Fpath & "\" & Fname
> Sheets(9).Copy after:=Workbooks("Master1.xls") _
> .Sheets(Workbooks("Master1.xls").Sheets.Count)
> Workbooks(Fname).Close savechanges:=False
> Fname = Dir
> Loop
>
> End Sub
>
>
> --
> Chris Lewis


 
Reply With Quote
 
Chris Lewis
Guest
Posts: n/a
 
      14th Nov 2006

"bac" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Chris, Bob & Mark,
>
> You are the Bomb! Thanks Chris for noticing the missing \. It works
> GREAT! Just what I was looking for.
>
> THANK YOU GUYS!
>



For short bits of code like this you can step through the code line by line
by pressing F8 when in the VBA editor window. If you have the Locals
Windows displayed (View | Locals Window in the VBA editor) you can watch the
value of each variable to check they are being assigned the value you expect
at that point.

I find it invaluable for both finding errors and also trying to understand
others code that I am struggling with.
--
Chris Lewis


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt User to Select Macro to Run? SeventhFloorProfessor Microsoft Excel Programming 1 15th May 2009 07:03 PM
RE: Prompt User to Select Macro to Run? joel Microsoft Excel Programming 1 15th May 2009 05:00 PM
Re: Prompt User to Select Macro to Run? Patrick Molloy Microsoft Excel Programming 0 15th May 2009 04:34 PM
Re: Prompt User to Select Macro to Run? Jon Peltier Microsoft Excel Programming 0 15th May 2009 04:30 PM
How to Prompt a user to select a Folder. Akash Microsoft Excel Programming 3 5th Jul 2007 05:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:46 PM.