Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Ramius
 
Posts: n/a
Default How do I force an Excel macro to ask me which file and directory?

I recorded an Import External Data macro that I wish to use on other files.
It works beautifully, but only on that one specfic file in that one
directory. I can get the Visual Basic code for that macro to where I can
edit it, but don't know what to substitute for the directory/file so that the
macro is forced to ask.
  #2   Report Post  
Ken Wright
 
Posts: n/a
Default

Drop this into a module and run 'DoSomething' as an example

Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function

Sub DoSomething()
userfile = PickFolder(strStartDir)
If userfile = "" Then
MsgBox "Canceled"
End If

With Application.FileSearch
.SearchSubFolders = True
.NewSearch
.Filename = ".xls"
.LookIn = userfile
.FileType = msoFileTypeExcelWorkbooks
.Execute
ffc = .FoundFiles.Count
MsgBox ffc

End With
End Sub


--
Regards
Ken....................... Microsoft MVP - Excel
Sys Spec - Win XP Pro / XL 97/00/02/03

----------------------------------------------------------------------------
It's easier to beg forgiveness than ask permission :-)
----------------------------------------------------------------------------

"Ramius" wrote in message
...
I recorded an Import External Data macro that I wish to use on other

files.
It works beautifully, but only on that one specfic file in that one
directory. I can get the Visual Basic code for that macro to where I can
edit it, but don't know what to substitute for the directory/file so that

the
macro is forced to ask.



  #3   Report Post  
Ramius
 
Posts: n/a
Default

I've been looking through other posts and it looks like a lot of times it is
helpful when the code is included. I think the pertinent parts of the code
are as follows:

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\My Documents\capture.txt", Destination:=Range("A1"))
.Name = "CAPTURE"

where "D:\My Documents\capture.txt" is the location/file is recorded the
macro with.

"Ramius" wrote:

I recorded an Import External Data macro that I wish to use on other files.
It works beautifully, but only on that one specfic file in that one
directory. I can get the Visual Basic code for that macro to where I can
edit it, but don't know what to substitute for the directory/file so that the
macro is forced to ask.

  #4   Report Post  
Dave Peterson
 
Posts: n/a
Default

I bet Ken wanted at least one extra line:

If userfile = "" Then
MsgBox "Canceled"
End If

to
If userfile = "" Then
MsgBox "Canceled"
Exit Sub
End If



Ken Wright wrote:

Drop this into a module and run 'DoSomething' as an example

Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function

Sub DoSomething()
userfile = PickFolder(strStartDir)
If userfile = "" Then
MsgBox "Canceled"
End If

With Application.FileSearch
.SearchSubFolders = True
.NewSearch
.Filename = ".xls"
.LookIn = userfile
.FileType = msoFileTypeExcelWorkbooks
.Execute
ffc = .FoundFiles.Count
MsgBox ffc

End With
End Sub

--
Regards
Ken....................... Microsoft MVP - Excel
Sys Spec - Win XP Pro / XL 97/00/02/03

----------------------------------------------------------------------------
It's easier to beg forgiveness than ask permission :-)
----------------------------------------------------------------------------

"Ramius" wrote in message
...
I recorded an Import External Data macro that I wish to use on other

files.
It works beautifully, but only on that one specfic file in that one
directory. I can get the Visual Basic code for that macro to where I can
edit it, but don't know what to substitute for the directory/file so that

the
macro is forced to ask.


--

Dave Peterson
  #5   Report Post  
Ken Wright
 
Posts: n/a
Default

Dohhhhhhh - Cheers for the catch Dave :-)

--
Regards
Ken....................... Microsoft MVP - Excel
Sys Spec - Win XP Pro / XL 97/00/02/03

----------------------------------------------------------------------------
It's easier to beg forgiveness than ask permission :-)
----------------------------------------------------------------------------

"Dave Peterson" wrote in message
...
I bet Ken wanted at least one extra line:

If userfile = "" Then
MsgBox "Canceled"
End If

to
If userfile = "" Then
MsgBox "Canceled"
Exit Sub
End If



Ken Wright wrote:

Drop this into a module and run 'DoSomething' as an example

Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function

Sub DoSomething()
userfile = PickFolder(strStartDir)
If userfile = "" Then
MsgBox "Canceled"
End If

With Application.FileSearch
.SearchSubFolders = True
.NewSearch
.Filename = ".xls"
.LookIn = userfile
.FileType = msoFileTypeExcelWorkbooks
.Execute
ffc = .FoundFiles.Count
MsgBox ffc

End With
End Sub

--
Regards
Ken....................... Microsoft MVP - Excel
Sys Spec - Win XP Pro / XL 97/00/02/03


--------------------------------------------------------------------------

--
It's easier to beg forgiveness than ask permission :-)


--------------------------------------------------------------------------

--

"Ramius" wrote in message
...
I recorded an Import External Data macro that I wish to use on other

files.
It works beautifully, but only on that one specfic file in that one
directory. I can get the Visual Basic code for that macro to where I

can
edit it, but don't know what to substitute for the directory/file so

that
the
macro is forced to ask.


--

Dave Peterson



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
Help... File Not Saved SteveHoot Excel Discussion (Misc queries) 0 January 5th 05 10:45 AM
Problems opening Excel files using DFS links Byron Excel Discussion (Misc queries) 2 January 1st 05 11:31 PM
Macro - - Automation Jac Excel Discussion (Misc queries) 8 December 27th 04 02:42 PM
Creating a PDF file programatically from Excel Chaplain Doug Excel Discussion (Misc queries) 0 December 16th 04 08:49 PM
This one is tricky....Macro to save file as cell value x in di Andy Excel Discussion (Misc queries) 4 November 26th 04 08:52 AM


All times are GMT +1. The time now is 06:31 AM.

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

About Us

"It's about Microsoft Excel"