簡體   English   中英

使用新工作簿而不是新工作表進行CSV文件轉換

[英]CSV file conversion with New Workbook instead of New Sheet

大家好,大家可以幫我查詢一下嗎? 在下面的VBA代碼中,它執行一個程序,將csv文件轉換為另一個工作表(SHEET2)中的excel文件但是在同一個WORKBOOK中

我想知道是否有任何方法可以改變它而不是進入SHEET2的CSV文件,excel可以提示我創建一個新的EXCEL WORKBOOK並詢問工作簿的名稱以及可能的新位置。

注意:下面的代碼只是整個代碼塊中的一個塊,用於執行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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM