Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Problem with an amended macro. Please help!

I have tried to amend this macro to use myself but its not working. can
anyone see why this would not be working? i am trying to open a folder
'folderName' and loop through, converting all csv files to xls files.

thanks, dave



Sub CSVToXls()

Dim folderName As String
folderName = GetFolderName("Select a folder")

If folderName = "" Then
MsgBox "You Didn't Select A Folder."
Else

End If

Application.DisplayAlerts = False
myFile = ActiveWorkbook.Name
myPath = foldername
WorkFile = Dir(myPath & "*.CSV")

Do While WorkFile < ""
Application.StatusBar = "Now working on " & WorkFile
Workbooks.Open Filename:=myPath & WorkFile


ActiveWorkbook.SaveAs Filename:=myPath & _
Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4),
FileFormat:=xlNormal
ActiveWorkbook.Close
Windows(myFile).Activate
WorkFile = Dir()
Loop

Application.StatusBar = False

End Sub


Note: I have the following code also to allow for the GetFolderName


Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As
String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim x As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
x = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
End Function


  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,355
Default Problem with an amended macro. Please help!

I see a couple of possible problems.

For this:

WorkFile = Dir(myPath & "*.CSV")

Make sure that myPath has a "\" at the end

Also change this

Workbooks.Open Filename:=myPath & WorkFile

to this

Set oWB = Workbooks.Open Filename:=myPath & WorkFile

And do all your action on oWB
--
HTH,
Barb Reinhardt



"Dave" wrote:

I have tried to amend this macro to use myself but its not working. can
anyone see why this would not be working? i am trying to open a folder
'folderName' and loop through, converting all csv files to xls files.

thanks, dave



Sub CSVToXls()

Dim folderName As String
folderName = GetFolderName("Select a folder")

If folderName = "" Then
MsgBox "You Didn't Select A Folder."
Else

End If

Application.DisplayAlerts = False
myFile = ActiveWorkbook.Name
myPath = foldername
WorkFile = Dir(myPath & "*.CSV")

Do While WorkFile < ""
Application.StatusBar = "Now working on " & WorkFile
Workbooks.Open Filename:=myPath & WorkFile


ActiveWorkbook.SaveAs Filename:=myPath & _
Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4),
FileFormat:=xlNormal
ActiveWorkbook.Close
Windows(myFile).Activate
WorkFile = Dir()
Loop

Application.StatusBar = False

End Sub


Note: I have the following code also to allow for the GetFolderName


Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As
String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim x As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
x = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
End Function


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Problem with an amended macro. Please help!

Where do I put the \ on myPath?

Thanks
Dave

"Barb Reinhardt" wrote:

I see a couple of possible problems.

For this:

WorkFile = Dir(myPath & "*.CSV")

Make sure that myPath has a "\" at the end

Also change this

Workbooks.Open Filename:=myPath & WorkFile

to this

Set oWB = Workbooks.Open Filename:=myPath & WorkFile

And do all your action on oWB
--
HTH,
Barb Reinhardt



"Dave" wrote:

I have tried to amend this macro to use myself but its not working. can
anyone see why this would not be working? i am trying to open a folder
'folderName' and loop through, converting all csv files to xls files.

thanks, dave



Sub CSVToXls()

Dim folderName As String
folderName = GetFolderName("Select a folder")

If folderName = "" Then
MsgBox "You Didn't Select A Folder."
Else

End If

Application.DisplayAlerts = False
myFile = ActiveWorkbook.Name
myPath = foldername
WorkFile = Dir(myPath & "*.CSV")

Do While WorkFile < ""
Application.StatusBar = "Now working on " & WorkFile
Workbooks.Open Filename:=myPath & WorkFile


ActiveWorkbook.SaveAs Filename:=myPath & _
Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4),
FileFormat:=xlNormal
ActiveWorkbook.Close
Windows(myFile).Activate
WorkFile = Dir()
Loop

Application.StatusBar = False

End Sub


Note: I have the following code also to allow for the GetFolderName


Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As
String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim x As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
x = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
End Function


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,355
Default Problem with an amended macro. Please help!

I'd also put a oWB.close before the loop or else you could have a whole lot
of docs open.
--
HTH,
Barb Reinhardt



"Dave" wrote:

I have tried to amend this macro to use myself but its not working. can
anyone see why this would not be working? i am trying to open a folder
'folderName' and loop through, converting all csv files to xls files.

thanks, dave



Sub CSVToXls()

Dim folderName As String
folderName = GetFolderName("Select a folder")

If folderName = "" Then
MsgBox "You Didn't Select A Folder."
Else

End If

Application.DisplayAlerts = False
myFile = ActiveWorkbook.Name
myPath = foldername
WorkFile = Dir(myPath & "*.CSV")

Do While WorkFile < ""
Application.StatusBar = "Now working on " & WorkFile
Workbooks.Open Filename:=myPath & WorkFile


ActiveWorkbook.SaveAs Filename:=myPath & _
Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4),
FileFormat:=xlNormal
ActiveWorkbook.Close
Windows(myFile).Activate
WorkFile = Dir()
Loop

Application.StatusBar = False

End Sub


Note: I have the following code also to allow for the GetFolderName


Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As
String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim x As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
x = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
End Function


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,355
Default Problem with an amended macro. Please help!

Make this modification

myPath = foldername '<~~~you already have this
if not right(mypath,1) = "\" then
mypath = mypath & "\"
end if

--
HTH,
Barb Reinhardt



"Dave" wrote:

Where do I put the \ on myPath?

Thanks
Dave

"Barb Reinhardt" wrote:

I see a couple of possible problems.

For this:

WorkFile = Dir(myPath & "*.CSV")

Make sure that myPath has a "\" at the end

Also change this

Workbooks.Open Filename:=myPath & WorkFile

to this

Set oWB = Workbooks.Open Filename:=myPath & WorkFile

And do all your action on oWB
--
HTH,
Barb Reinhardt



"Dave" wrote:

I have tried to amend this macro to use myself but its not working. can
anyone see why this would not be working? i am trying to open a folder
'folderName' and loop through, converting all csv files to xls files.

thanks, dave



Sub CSVToXls()

Dim folderName As String
folderName = GetFolderName("Select a folder")

If folderName = "" Then
MsgBox "You Didn't Select A Folder."
Else

End If

Application.DisplayAlerts = False
myFile = ActiveWorkbook.Name
myPath = foldername
WorkFile = Dir(myPath & "*.CSV")

Do While WorkFile < ""
Application.StatusBar = "Now working on " & WorkFile
Workbooks.Open Filename:=myPath & WorkFile


ActiveWorkbook.SaveAs Filename:=myPath & _
Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4),
FileFormat:=xlNormal
ActiveWorkbook.Close
Windows(myFile).Activate
WorkFile = Dir()
Loop

Application.StatusBar = False

End Sub


Note: I have the following code also to allow for the GetFolderName


Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As
String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim x As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
x = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
End Function


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
Problem with Macro ChuckW Excel Discussion (Misc queries) 0 March 28th 07 01:16 AM
SUMPRODUCT - (amended) Exclude LAST Row of Matched Criteria (Month & Year) Sam via OfficeKB.com Excel Worksheet Functions 2 January 9th 07 12:37 AM
Macro problem tweacle Excel Worksheet Functions 0 February 15th 06 08:26 PM
Macro problem....... Alec H Excel Discussion (Misc queries) 4 February 15th 06 11:38 AM
Pls disregard last post used the second amended suggestion from John Mark Stephens Charts and Charting in Excel 0 May 6th 05 06:41 PM


All times are GMT +1. The time now is 09:29 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"