处理一个单元格中的多个值以导出为CSV

时间:2016-12-30 16:27:16

标签: excel vba excel-vba csv export-to-csv

我正在尝试设计一个VBA脚本,将Excel电子表格中的值导出为CSV文件。这将用于通过单独的Powershell脚本部署多个虚拟机。

目前,导出到CSV操作正在处理每个单元的一个VM名称和相应的设置,但是,在脚本的实际执行中,我们可能在一个单元格中有多个VM名称(以逗号分隔)但是将具有前进单元格中的相同设置。

我想知道的是,是否可能/如何在该单元格中使用逗号分隔的那些值填充单独的行,但仍然在CSV文件中的相应标题下进行设置。< / p>

代码:

Function BuildValuesString(colIndex As String, rows As String) As String
    Dim val As Variant

    For Each val In Split(rows, ",")
        If Cells(val, colIndex) <> "" Then BuildValuesString = BuildValuesString & Cells(val, colIndex).Value & ","
    Next val
End Function
Function BuildNullStrings(numNullStrings As Long) As String
    Dim iNullStrings As Long

    For iNullStrings = 1 To numNullStrings
        BuildNullStrings = BuildNullStrings & "" & ","
    Next iNullStrings
End Function


Sub WriteCSVFile2()

    Dim My_filenumber As Integer
    Dim logSTR As String

    My_filenumber = FreeFile

    logSTR = logSTR & "Srv. Descr." & ","
    logSTR = logSTR & "E-mail" & ","
    logSTR = logSTR & "Env." & ","
    logSTR = logSTR & "Prot. Level" & ","
    logSTR = logSTR & "DataC" & ","
    logSTR = logSTR & "# VMs" & ","
    logSTR = logSTR & "Name" & ","
    logSTR = logSTR & "Cluster" & ","
    logSTR = logSTR & "VLAN" & ","
    logSTR = logSTR & "NumCPU" & ","
    logSTR = logSTR & "MemoryGB" & ","
    logSTR = logSTR & "C_System" & ","
    logSTR = logSTR & "D_Homevg" & ","
    logSTR = logSTR & "App_eHealth" & ","
    logSTR = logSTR & "TotalDisk" & ","
    logSTR = logSTR & "Datastore" & ","
    logSTR = logSTR & "Template" & ","

    logSTR = logSTR & Chr(13)

    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22,26,27,28,29,30,31,32,33,34")
    logSTR = logSTR & TotalIt(Range("C32:C33"))

    logSTR = logSTR & Chr(13)

    logSTR = logSTR & BuildNullStrings(5)
    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22,37,38,39,40,41,42,43,44,46")
    logSTR = logSTR & TotalIt(Range("C43:C44"))

    logSTR = logSTR & Chr(13)

    logSTR = logSTR & BuildNullStrings(5)
    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22,48,49,50,51,52,53,54,55,58")
    logSTR = logSTR & TotalIt(Range("C54:C55"))

Open "Z:\Operational Env. Requests\2016\Requests(Test)\" & ThisWorkbook.Name & ".csv" For Append As #My_filenumber
    Print #My_filenumber, logSTR
Close #My_filenumber

End Sub

Function TotalIt(rng As Range)
    Dim c, arr, t, rv As Double, v
    For Each c In rng.Cells
        If Len(c.Value) > 0 Then
            arr = Split(c.Value, ",")
            For Each v In arr
                If IsNumeric(v) Then rv = rv + v
            Next v
            Exit For
        End If
    Next c
    TotalIt = rv
End Function

即。 如果单元格C:27具有&#34; VM1,VM2,VM3&#34;,则将不同行上的每个值分开,但仍然输入与其他标题相关联的值。

目前,VBA脚本会将值输入为:

Name           Cluster     VLAN   NumCPU      MemoryGB
VM1             VM2         VM3   VMCluster   VLAN1 etc.

我希望它能做什么

Name  Cluster    VLAN
VM1   VMCluster  VLAN1
VM2   VMCluster  VLAN1
VM3   VMCluster  VLAN1
如果我尝试以当前格式执行VBA脚本,它会完全弄乱对齐。我希望得到一些关于如何实现预期结果的帮助。

感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:2)

这是根本过于简化的,但根据A-C列中名称,群集和VLAN的简化示例,这将采用该功能并将输出拆分为CSV。

Sub SplitToCsv()

  Dim rw, lastRow As Long
  Dim ws As Worksheet
  Dim vms, vm, rowData As Variant

  Set ws = Sheets("Sheet1")
  lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

  Open "C:\test.csv" For Output As #1

  For rw = 1 To lastRow
    vms = Split(ws.Cells(rw, 1).Value2, ",")
    For Each vm In vms
      Print #1, Join(Array(vm, ws.Cells(rw, 2).Value2, ws.Cells(rw, 3).Value2), ",")
    Next vm
  Next rw

  Close #1

End Sub

这会将此作为输入:

Name          Cluster     VLAN
VM1,VM2,VM3   VMCluster   VLAN1
VM4           VMCluster2  VLAN2
VM5,VM6       VMCluster3  VLAN3

然后把它变成一个看起来像这样的文件:

Name,Cluster,VLAN
VM1,VMCluster,VLAN1
VM2,VMCluster,VLAN1
VM3,VMCluster,VLAN1
VM4,VMCluster2,VLAN2
VM5,VMCluster3,VLAN3
VM6,VMCluster3,VLAN3

- 编辑1/3/2017 -

我想我现在明白了。这是您的代码的精简版/重构版。我可能错了,但我相信这是你正在尝试做的事情:

Sub WriteCSVFile2()

    Dim My_filenumber As Integer
    Dim header, line As String
    Dim values, vlan, names As Variant
    Dim row As Integer

    My_filenumber = FreeFile
    Open "c:\cdh\" & ThisWorkbook.Name & ".csv" For Append As #My_filenumber
    Print #My_filenumber, Join(Array("Srv. Descr.", "E-mail", "Env.", _
        "Prot. Level", "DataC", "# VMs", "Name", "Cluster", "VLAN", _
        "NumCPU", "MemoryGB", "C_System", "D_Homevg", "App_eHealth", _
        "TotalDisk", "Datastore", "Template"), ",")

    header = Join(Application.Transpose(Range("C18:C22").Value), ",")

    For row = 26 To 50 Step 12
      values = Application.Transpose(Range("C" & row & ":C" & row + 8).Value)
      names = values(2)
      For Each vlan In Split(names, ",")
        values(2) = vlan
        line = Join(values, ",")
        Print #My_filenumber, header & "," & line
      Next vlan
    Next row

    ' TotalIt(Range("C54:C55"))

    Close #My_filenumber

End Sub

一些说明:   - 我评论TotalIt只是因为我认为它有效并且没有专注于那部分   - 您的代码查看了第26,37,48行,但电子表格实际上有26,38,50的引用(另外一行)。我的代码反映了后者

我认为如果您添加更多配置选项,较小的代码库将更容易调试,并且希望更具可扩展性。

这是我的输出:

Srv. Descr.,E-mail,Env.,Prot. Level,DataC,# VMs,Name,Cluster,VLAN,...
Service,Email,Environment,Protection,Data Center,,VM1,VMCluster,VLAN1 etc.,,,,,
Service,Email,Environment,Protection,Data Center,,VM2,VMCluster,VLAN1 etc.,,,,,
Service,Email,Environment,Protection,Data Center,,VM3,VMCluster,VLAN1 etc.,,,,,