2016-11-14 98 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 
相关问题