2012-04-02 35 views
0

我正在運行一個宏,該宏會自動將csv文件導入到我的工作簿中的特定工作表中。但是,我想通過讓用戶選擇要導入的文件而不是讓宏自動獲取csv文件來增加更大的靈活性,因爲命名可能會改變以及目錄。我是VBA的新手,一直試圖更好地理解MsoFileDialogType和GetOpenFilename,但難以將概念/實現理解爲我的代碼。宏以提示用戶選擇CSV文件以導入到工作簿中的現有工作表

我最終想要的是讓用戶點擊工作簿前端的按鈕。提示消息以選擇要導入的第一個csv文件。該csv文件將被導入到工作簿temp1中的預先命名的工作表中。但是,由於數據文件成對出現,我希望用戶能夠在第一個文件之後選擇下一個csv文件到temp2中。

什麼我現在是:

Worksheets.Add 
ActiveSheet.Name = "temp1" 
With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;MAC Directory path here" _ 
     , Destination:=Range("A1")) 
     .Name = "temp 1 03.02.12" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = xlMacintosh 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = False 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
     1, 1, 1) 
     .Refresh BackgroundQuery:=False 
     .UseListObject = False 
End With 
ActiveSheet.Move after:=Worksheets(Worksheets.Count) 

謝謝。

回答

3

也許在這些線上。

Sub GetCSVList() 
Dim dlgOpen As FileDialog 
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker) 
With dlgOpen 
    .AllowMultiSelect = True 
    ''Start in 
    .InitialFileName = "Z:\docs\" 
    .Show 
End With 

For Each fname In dlgOpen.SelectedItems 
    ImportCSV fname 
Next 
End Sub 

Sub ImportCSV(fname) 
Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count)) 
ws.Name = "temp" & Worksheets.Count + 1 

With ws.QueryTables.Add(_ 
     Connection:="TEXT;" & fname, _ 
     Destination:=Range("A1")) 
    .Name = "Temp" & Worksheets.Count + 1 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .BackgroundQuery = True 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = xlMacintosh 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .Refresh BackgroundQuery:=False 
    '.UseListObject = False 
End With 
End Sub 
+0

嗨Remou,我試圖執行代碼,但遇到了這個錯誤:「未定義的用戶定義類型」,並強調這行 - >昏暗dlgOpen作爲FileDialog的 – VMO 2012-04-02 14:50:51

+0

哪個版本的Excel您使用的?您可以添加對Microsoft Office x.x對象庫的引用嗎? – Fionnuala 2012-04-02 15:14:07

+0

嗨Remou,我使用的是Mac Office 2011.我在工具 - >參考下,並確保Visual Basic for Applications,Microsoft Excel 14.0 Object Library和Microsoft Office 14.0 Object Library被選中。 – VMO 2012-04-02 15:17:45

相關問題