2014-05-16 32 views
0

我正在研究一個宏,對三個工作簿進行重新格式化和編輯。這三本工作手冊的名稱始終相同,並且來自同一個來源。他們以.csv格式到達。我想讓VBA將所有這三個工作簿作爲單獨的工作表導入到一本書中,並根據每個工作簿的標題中找到的字符串重命名這些工作表。有沒有簡單的方法將它附加到錄製的宏?此外,是否有更好的方式導入和分隔/格式化文件,而不是通過記錄宏生成的方式?我已經把代碼從下面這個方法:使用VBA導入和重命名.CSV文件

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "FAKENAME.csv" _ 
    , Destination:=Range("$A$1")) 
    .CommandType = 0 
    .Name = "FAKENAME" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 65001 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, _ 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ 
    , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 
+0

[你可以從這裏開始了另一種方法](http://stackoverflow.com/questions/21010080/copying-data-from-a-text-file-to-an-excel-workbook/21010921#21010921)。該鏈接提供了加載多個文本文件(我認爲也適用於CSV)的解決方案,然後將其複製到工作表中。 – L42

+0

@ l42謝謝,我來看看。 – 114

回答

1

我提出以下爲打開CSV並在將其添加到輸出Workbook更簡單的方法:

Option Explicit 
Sub ImportCSVsToSheets() 

Dim File As String, Path As String 
Dim CSV As Workbook, Book As Workbook 
Dim CS As Worksheet, Sheet As Worksheet 
Dim LastRow As Long, LastCol As Long 
Dim Source As Range, Target As Range 

'set references up-front 
Application.DisplayAlerts = False 
Path = "c:\my\csv\files\" 
File = Dir(Path & "*.csv") 
Set Book = Workbooks.Add 

'output workbook setup, make it bare-bones by deleting all non-first sheets 
For Each Sheet In Book.Worksheets 
    If Sheet.Index <> 1 Then 
     Sheet.Delete 
    End If 
Next Sheet 
Set Sheet = Book.Worksheets(1) 
Sheet.Name = "DeleteMeSoon" 

'loop through the CSVs and write data to sheets in output book 
Do While Len(File) > 0 
    'set up CSV and determine copy range 
    Set CSV = Workbooks.Open(Path & File) 
    Set CS = CSV.ActiveSheet 
    With CS 
     LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
     LastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 
     Set Source = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) 
    End With 

    'set up new sheet and destination range 
    Set Sheet = Book.Worksheets.Add 
    With Sheet 
     Set Target = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) 
    End With 

    'copy data from CSV to target 
    Source.Copy Target 

    'set the sheet name using the CSV file name 
    Sheet.Name = Left(Left(File, Len(File) - 4), 31) 

    'close the CSV and repeat 
    CSV.Close SaveChanges:=False 
    File = Dir 
Loop 

'remove that last pesky sheet 
Set Sheet = Book.Worksheets("DeleteMeSoon") 
Sheet.Delete 

'save it however you'd like and boom we're done 
'Book.SaveAs Filname:="a-file-name", FileFormat:=xlWhatever 
Application.DisplayAlerts = True 
End Sub 
+0

這看起來不錯,但我唯一擔心的是打開.csv文件而不是導入它們,這可能會導致數據出現問題。例如,如果文件被打開而不是導入並更改爲相關日期類型(在這種情況下爲MDY),日期通常會格式錯誤(以我不知道如何修復的方式)。 – 114

+0

嗯......固定CSV內的值似乎不是原始範圍的一部分。但是,遲早,如果你的數據要存在於Excel中,它將需要以某種方式格式化(作爲它的聲音的日期)。我相信我們可以提供幫助 - 也許在一個新的問題上? –

+0

聽起來不錯。這也可能是我錯過了一種確保日期格式正確的方法,儘管我已經嘗試了許多標準方法,但沒有運氣,並且之前已建議我應該始終導入並且不打開.csv文件。我將在新帖子中將其鏈接。 – 114