简体   繁体   中英

CSV file conversion with New Workbook instead of New Sheet

Good day everyone, Can Anyone help me with my query? In the VBA Codes below, it executes a program to convert csv files into excel files in a different sheet (SHEET2) BUT in same WORKBOOK .

I am wondering if there is any way I can change that instead of the CSV files going into SHEET2, the excel can prompt me to CREATE A NEW EXCEL WORKBOOK and asks for the name of the Workbook and possibly its new location.

Note: The code below is just a block from the whole block of codes to execute the conversion of CSV file to Excel File. Reason is it is very long and I think that the programs preceeding the one's below is irrelevant to my real question.

So, if anyone needs the whole code I can just send it to your emails :)

Thank you!

Sub ImportFile()
Dim sPath As String
Dim intCoice As Integer
Dim strPath As String
Dim FilePath As Sting

'change the display name of the open file dialog
Application.FileDialog(msoFileDialogOpen).Title = _
    "CSV File Opener"

'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear

'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
    "CSV Files Only", "*.csv")

'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False

'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show

If intChoice <> 0 Then

    'get the file path selected by the user
    strPath = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)


Else
    MsgBox "Wrong CSV File. Please Choose Again"

End If

'Below we assume that the file, csvtest.csv,
'is in the same folder as the workbook. If
'you want something more flexible, you can
'use Application.GetOpenFilename to get a
'file open dialogue that returns the name
'of the selected file.
'On the page Fast text file import
'I show how to do that - just replace the
'file pattern "txt" with "csv".
sPath = ThisWorkbook.Path & strPath

'Procedure call. Semicolon is defined as separator,
'and data is to be inserted on "Sheet2".
'Of course you could also read the separator
'and sheet name from the worksheet or an input
'box. There are several options.

***copyDataFromCsvFileToSheet sPath, ";", "Sheet2"***

End Sub

Opening a .csv into a new workbook is fairly straightforward:

Dim InData As Workbook
Set InData = Workbooks.Open(strPath & MyFilename & ".csv")    'open the csv
'do stuff with InData

In your case, I believe strPath will be set to the complete path & filename the user selected from the file open dialog, so you'd need to remove & MyFilename & ".csv" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM