【问题标题】:Adding Headers to Exported CSV file from Excel Worksheet从 Excel 工作表向导出的 CSV 文件添加标题
【发布时间】:2016-11-14 13:19:04
【问题描述】:

我正在寻找一种解决方案,可以将一些预先确定的标题添加到从 Excel 工作表导出的 CSV 文件中。我正在应用 BruceWayne 添加的解决方案,当我将 VBA 代码应用到我当前的 Excel 工作表时,它会将标题添加到 Excel 电子表格本身。

我正在尝试找到一种方法将此 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

当我通过删除一个或另一个末尾的“End Sub”将两个 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 代码?

感谢您提供的任何信息

【问题讨论】:

  • 你能发布这个错误的内容吗?
  • 错误读取 - 编译错误:预期结束子
  • 您是否也删除了新宏的开头,例如Sub AddHeaders()
  • 我只是尝试按照您的建议进行操作 - 2 个组合的 VBA 代码能够成功执行,但标题仍应用于 Excel 工作表,而不是导出的 CSV 文档。

标签: vba excel csv macros


【解决方案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 & " , "

【讨论】:

  • 感谢您的回答,我只需要在标题名称末尾添加一个逗号即可将它们输入到不同的单元格中:“logSTR = logSTR & “Header1”。如何指定标题仅占用第 1 行,而从 Excel 电子表格中提取的剩余单元格数据填充第 2 行?
  • 是的,抱歉,我已经解决了。在最后一个标题之后添加 Chr(13),它会做你想做的。
  • 一切都好 - 我非常感谢您的帮助。我已经能够应用上述解决方案并达到预期的效果。我遇到了另一个问题,我不确定是否应该打开一个新问题来解决或者这是一个快速修复 - 有没有办法添加到 VBA 代码以让 CSV 导出“忽略”空白数据单元格?例如:如果 logSTR = logSTR & Cells(36, "C").Value & " , " 的数据为空白,则继续输入 logSTR = logSTR & Cells(37, "C").Value & " 的数据, " 进入该单元格,而不是将其留空并跳到下一个单元格?
  • 那么,您不在乎单元格是否与标题对齐?我的意思是,如果 C36 为空白,而您跳过它,那么 C37 将不再在同一列中,并且将位于错误的标题下。可以吗?
  • 是的,我想是的——我最终会放入空单元格以将数据调整到适用的列下。我最终发布了更详细的情况说明以及我的目标,并且一些人提出了一些建议:stackoverflow.com/questions/40593460/…
【解决方案2】:

您可以尝试将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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2021-10-24
    • 2015-07-12
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多