我正在尋找解決方案來處理將某些預定義標題添加到從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代碼?
謝謝你的任何信息,您可以提供
你可以張貼此錯誤的內容行的前面? – Limak
錯誤讀取 - 編譯錯誤:期望結束Sub – NeedToKnowBasis22
您是否也刪除了新宏的開始,例如'Sub AddHeaders()'? – Limak