从Excel工作表添加标题到导出的CSV文件

我正在寻找解决scheme来处理将一些预定义的标题添加到从Excel工作表中导出的CSV文件。 我正在申请由BruceWayne添加的解决scheme,当我将VBA代码应用到当前的Excel工作表时,它将标题添加到Excel电子表格本身。

我试图find一种方法来将这个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 

当我将两个VBA代码组合在一起时,通过删除一个或另一个末尾的'End Sub',我收到一个错误,并且必须重新添加该行才能成功应用代码; 然而,在这样做的代码必须分开申请,然后标题被添加到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代码?

感谢您提供的任何信息

AddHeaders旨在将一行标题添加到工作表。 你不会简单地通过使其成为另一个子程序的一部分来改变它的function。 WriteCSVFile将一系列值写入文本文件,以创buildCSV。 你不能很容易地把这两者结合起来,但是你可以把前面的一些代码添加到后者中,像这样。

 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 & " , " 

你可以尝试Close第二个macros的结尾?

 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