Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 234
Default Find file and remove .csv extension

Thank you both, two solutions I will work with to reach my objective.

Many thanks for the prompt reply. Rob

"Dave Peterson" wrote in message
...
VBA has its own equivalent of =Upper(), =Find(), =left(), so you don't
need to
use the worksheetfunctions:

Option Explicit
Sub macro1()

Dim myFilter As String
Dim myCaption As String
Dim SelectedFile As Variant 'could be false if user cancels
Dim NewName As String
Dim TheUser As String

TheUser = "hi there"

myFilter = "Text files,*.csv"
myCaption = "Please Select a File " & TheUser

SelectedFile = Application.GetOpenFilename(fileFilter:=myFilter, _
Title:=myCaption)

If SelectedFile = False Then
Exit Sub 'user hit cancel
End If

If UCase(Right(SelectedFile, 4)) = UCase(".csv") Then
NewName = Left(SelectedFile, Len(SelectedFile) - 4) & ".txt"
Name SelectedFile As NewName
End If

End Sub

============
When I'm doing this kind of thing, I'll usually just copy the file to a
.txt,
open it, copy it to where I want, close the text workbook, and delete the
.txt
file.

That way, I don't have to worry about modifying that .csv file.

Option Explicit
Sub macro1()

Dim myFilter As String
Dim myCaption As String
Dim SelectedFile As Variant 'could be false if user cancels
Dim NewName As String
Dim TheUser As String
Dim DestCell As Range
Dim TempWks As Worksheet

myFilter = "Text files,*.csv"
myCaption = "Please Select a File " & TheUser

SelectedFile = Application.GetOpenFilename(fileFilter:=myFilter, _
Title:=myCaption)

If SelectedFile = False Then
Exit Sub 'user hit cancel
End If

Set DestCell = Worksheets.Add.Range("A1")

NewName = SelectedFile & ".txt"
FileCopy Source:=SelectedFile, Destination:=NewName

Workbooks.OpenText Filename:=NewName 'and all the other parsing stuff
here

Set TempWks = ActiveSheet 'the worksheet in the new text workbook

TempWks.Cells.Copy _
Destination:=DestCell

TempWks.Parent.Close savechanges:=False

Kill NewName

End Sub



Rob wrote:

Hi,

I'm tring to open a window, select a file with CSV extension, the file is
coded to SelectedFile and then remove I want to remove the .CSV before
renaming the file with the same name albeit with a .TXT extension.

I've tried the code below for the inital test but it fails on the
WorksheetFunction code.

Am I on the right track?

Thanks, Rob
Sub macro1()

Dim Filter, Caption, SelectedFile, DestinationFile As String

Filter = "Text files (*.csv),*.csv"
Caption = "Please Select a File " & TheUser
SelectedFile = Application.GetOpenFilename(Filter, , Caption)

SelectedFile = Application.WorksheetFunction _
.Left(SelectedFile, Find(".CSV", (.Upper(SelectedFile)), 1) - 1)

Debug.Print SelectedFile

End Sub


--

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
Retrieving Excel File extension based on XL File Format Enumeratio Sasikumar Kannappan Excel Programming 2 June 24th 09 03:59 PM
file format or file extension is not valid...error message Ballun Excel Discussion (Misc queries) 0 May 7th 09 09:06 PM
Additional file with no extension created during File Save As proc Peter Rooney Excel Discussion (Misc queries) 2 August 11th 05 02:48 PM
Remove extension Stanley Lam Excel Programming 5 May 9th 05 01:31 PM
I need to download an exel spreadsheet file. (file extension :xls) buckrogers Excel Discussion (Misc queries) 2 December 8th 04 11:08 PM


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

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"