2016-11-14 88 views
0

我正在尋找解決方案來處理將某些預定義標題添加到從Excel工作表中導出的CSV文件。我正在應用由BruceWayne添加的解決方案,並且當我將VBA代碼應用於當前的Excel工作表時,它將標題添加到Excel電子表格本身。從Excel工作表中將標題添加到導出的CSV文件

我想找到一種方法來將此VBA代碼應用於導出的CSV文件本身,而不是Excel電子表格。我的VBA代碼目前看起來這樣:

Sub WriteCSVFile() 

Dim My_filenumber As Integer 
Dim logSTR As String 

My_filenumber = FreeFile 

logSTR = logSTR & Cells(18, "C").Value & " , " 
logSTR = logSTR & Cells(19, "C").Value & " , " 
logSTR = logSTR & Cells(20, "C").Value & " , " 
logSTR = logSTR & Cells(21, "C").Value & " , " 
logSTR = logSTR & Cells(22, "C").Value & " , " 
logSTR = logSTR & Cells(26, "C").Value & " , " 
logSTR = logSTR & Cells(27, "C").Value & " , " 
logSTR = logSTR & Cells(28, "C").Value & " , " 
logSTR = logSTR & Cells(29, "C").Value & " , " 
logSTR = logSTR & Cells(30, "C").Value & " , " 
logSTR = logSTR & Cells(31, "C").Value & " , " 
logSTR = logSTR & Cells(32, "C").Value & " , " 
logSTR = logSTR & Cells(36, "C").Value & " , " 
logSTR = logSTR & Cells(37, "C").Value & " , " 
logSTR = logSTR & Cells(38, "C").Value & " , " 
logSTR = logSTR & Cells(39, "C").Value & " , " 
logSTR = logSTR & Cells(40, "C").Value & " , " 
logSTR = logSTR & Cells(41, "C").Value & " , " 
logSTR = logSTR & Cells(42, "C").Value & " , " 
logSTR = logSTR & Cells(46, "C").Value & " , " 
logSTR = logSTR & Cells(47, "C").Value & " , " 
logSTR = logSTR & Cells(48, "C").Value & " , " 
logSTR = logSTR & Cells(49, "C").Value & " , " 
logSTR = logSTR & Cells(50, "C").Value & " , " 
logSTR = logSTR & Cells(51, "C").Value & " , " 
logSTR = logSTR & Cells(52, "C").Value & " , " 

Open "Z:\SHARE DRIVE\RequestDirectory\" & ThisWorkbook.Name & ".csv" For Append As #My_filenumber 
    Print #My_filenumber, logSTR 
Close #My_filenumber 

End Sub 

當我通過在一個或另一個的端除去「結束子」結合2個VBA碼一起,收到錯誤,必須重新添加線讓代碼成功應用;然而,在做這樣的代碼必須分開申請,然後頭被添加到Excel工作表本身:

Sub AddHeaders() 
Dim headers() As Variant 
Dim ws As Worksheet 
Dim wb As Workbook 

Application.ScreenUpdating = False 'turn this off for the macro to run a little faster 

Set wb = ActiveWorkbook 

headers() = Array("Header1", "Header2", "Header3", "Header4", "Header5", "Header6", 
    "Header7", "Header8", "Header9", "Header10", "Header11", "Header12") 
For Each ws In wb.Sheets 
    With ws 
    .Rows(1).Value = "" 'This will clear out row 1 
    For i = LBound(headers()) To UBound(headers()) 
     .Cells(1, 1 + i).Value = headers(i) 
    Next i 
    .Rows(1).Font.Bold = True 
    End With 
Next ws 

Application.ScreenUpdating = True 'turn it back on 

MsgBox ("Done!") 


End Sub 

我想知道是否有應用該處理的中添加VBA代碼的簡單方法頭文件以及從Excel工作表導出到CSV文件的數據,而不分離2個VBA代碼?

謝謝你的任何信息,您可以提供

+0

你可以張貼此錯誤的內容行的前面? – Limak

+0

錯誤讀取 - 編譯錯誤:期望結束Sub – NeedToKnowBasis22

+0

您是否也刪除了新宏的開始,例如'Sub AddHeaders()'? – Limak

回答

1

AddHeaders被設計爲標題的行添加到工作表。您不會簡單地通過將其作爲其他子例程的一部分來改變它的功能。 WriteCSVFile將一系列值寫入文本文件,以創建CSV。你不能輕易地將這兩者結合起來,但你可以將前者的一些代碼添加到後者中,就像這樣。

logSTR = logSTR & "header1" & " , " 
logSTR = logSTR & "header2" & " , " 
logSTR = logSTR & "header3" & " , " 
logSTR = logSTR & "header4" & " , " 
'continue for each header 

logSTR = logSTR & Chr(13) 

添加該代碼在說

logSTR = logSTR & Cells(18, "C").Value & " , " 
+0

感謝您的回答,我只需要在標題名稱末尾添加一個逗號,以便將它們輸入到不同的單元格中:「logSTR = logSTR&」Header1,「。是否有方法指定僅標題佔據第1行,而從Excel電子表格中提取的剩餘單元格數據填充第2行? – NeedToKnowBasis22

+0

是的,很抱歉,我修正了這個問題。在最後一個標題後面添加Chr(13),它會按照你的意願做 – Hrothgar

+0

All good - 我非常感謝這方面的幫助,我已經能夠應用上述解決方案並取得了預期的結果,但我遇到了另一個問題,我不確定是否應該提出一個新問題來解決這個問題,或者如果這是一個快速解決方案 - 例如:如果來自logSTR = logSTR&Cells(36,「C」).Value&「,」的數據是空白的,那麼有沒有辦法添加到VBA代碼中來讓CSV導出「忽略」空白數據單元?繼續輸入來自logSTR = logSTR&Cells(37,「C」)的數據。Value&「,」i nto細胞而不是留下空白並跳到下一個細胞? – NeedToKnowBasis22

0

你可以嘗試把Close第二宏德結束了嗎?

Sub WriteCSVFile() 

Dim My_filenumber As Integer 
Dim logSTR As String 

My_filenumber = FreeFile 

logSTR = logSTR & Cells(18, "C").Value & " , " 
logSTR = logSTR & Cells(19, "C").Value & " , " 
logSTR = logSTR & Cells(20, "C").Value & " , " 
logSTR = logSTR & Cells(21, "C").Value & " , " 
logSTR = logSTR & Cells(22, "C").Value & " , " 
logSTR = logSTR & Cells(26, "C").Value & " , " 
logSTR = logSTR & Cells(27, "C").Value & " , " 
logSTR = logSTR & Cells(28, "C").Value & " , " 
logSTR = logSTR & Cells(29, "C").Value & " , " 
logSTR = logSTR & Cells(30, "C").Value & " , " 
logSTR = logSTR & Cells(31, "C").Value & " , " 
logSTR = logSTR & Cells(32, "C").Value & " , " 
logSTR = logSTR & Cells(36, "C").Value & " , " 
logSTR = logSTR & Cells(37, "C").Value & " , " 
logSTR = logSTR & Cells(38, "C").Value & " , " 
logSTR = logSTR & Cells(39, "C").Value & " , " 
logSTR = logSTR & Cells(40, "C").Value & " , " 
logSTR = logSTR & Cells(41, "C").Value & " , " 
logSTR = logSTR & Cells(42, "C").Value & " , " 
logSTR = logSTR & Cells(46, "C").Value & " , " 
logSTR = logSTR & Cells(47, "C").Value & " , " 
logSTR = logSTR & Cells(48, "C").Value & " , " 
logSTR = logSTR & Cells(49, "C").Value & " , " 
logSTR = logSTR & Cells(50, "C").Value & " , " 
logSTR = logSTR & Cells(51, "C").Value & " , " 
logSTR = logSTR & Cells(52, "C").Value & " , " 

Open "Z:\SHARE DRIVE\RequestDirectory\" & ThisWorkbook.Name & ".csv" For Append As #My_filenumber 
    Print #My_filenumber, logSTR 

Dim headers() As Variant 
Dim ws As Worksheet 
Dim wb As Workbook 

Application.ScreenUpdating = False 'turn this off for the macro to run a little faster 

Set wb = ActiveWorkbook 

headers() = Array("Header1", "Header2", "Header3", "Header4", "Header5", "Header6", 
    "Header7", "Header8", "Header9", "Header10", "Header11", "Header12") 
For Each ws In wb.Sheets 
    With ws 
    .Rows(1).Value = "" 'This will clear out row 1 
    For i = LBound(headers()) To UBound(headers()) 
     .Cells(1, 1 + i).Value = headers(i) 
    Next i 
    .Rows(1).Font.Bold = True 
    End With 
Next ws 

Application.ScreenUpdating = True 'turn it back on 
Close #My_filenumber 

End Sub 
相關問題