Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用新工作簿而不是新工作表转换CSV文件_Vba_Excel_Csv - Fatal编程技术网

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

Vba 使用新工作簿而不是新工作表转换CSV文件,vba,excel,csv,Vba,Excel,Csv,大家好,, 有人能帮我问一下吗? 在下面的VBA代码中,它执行一个程序,将csv文件转换为同一工作簿中不同工作表2中的excel文件 我想知道是否有什么方法可以改变,excel可以提示我创建一个新的excel工作簿,并询问工作簿的名称和可能的新位置,而不是将CSV文件放入SHEET2 注意:下面的代码只是执行CSV文件到Excel文件转换的整个代码块中的一个块。原因是它很长,我认为在下面一个之前的程序与我真正的问题无关 因此,如果有人需要全部代码,我可以将其发送到您的电子邮件: 谢谢大家! Su

大家好,, 有人能帮我问一下吗? 在下面的VBA代码中,它执行一个程序,将csv文件转换为同一工作簿中不同工作表2中的excel文件

我想知道是否有什么方法可以改变,excel可以提示我创建一个新的excel工作簿,并询问工作簿的名称和可能的新位置,而不是将CSV文件放入SHEET2

注意:下面的代码只是执行CSV文件到Excel文件转换的整个代码块中的一个块。原因是它很长,我认为在下面一个之前的程序与我真正的问题无关

因此,如果有人需要全部代码,我可以将其发送到您的电子邮件:

谢谢大家!

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

在新工作簿中打开.csv非常简单:

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

在您的情况下,我相信strPath将设置为用户从“文件打开”对话框中选择的完整路径和文件名,因此您需要删除&MyFilename&.csv。

要将csv文件保存为现有工作簿中的新工作表,您需要将已打开的csv文件复制到目标工作簿中。谢谢。它可以工作,但文件不是转换后的格式,因为程序处理CopyDataFromCsvFileToSheet命令。非常感谢。