简体   繁体   English

使用新工作簿而不是新工作表进行CSV文件转换

[英]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 . 在下面的VBA代码中,它执行一个程序,将csv文件转换为另一个工作表(SHEET2)中的excel文件但是在同一个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. 我想知道是否有任何方法可以改变它而不是进入SHEET2的CSV文件,excel可以提示我创建一个新的EXCEL WORKBOOK并询问工作簿的名称以及可能的新位置。

Note: The code below is just a block from the whole block of codes to execute the conversion of CSV file to Excel File. 注意:下面的代码只是整个代码块中的一个块,用于执行CSV文件到Excel文件的转换。 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: 将.csv打开到新工作簿中非常简单:

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" . 在您的情况下,我相信strPath将设置为用户从文件打开对话框中选择的完整路径和文件名,因此您需要删除& MyFilename & ".csv"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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