我想設計一個VBA腳本,它將從Excel電子表格中將值導出爲CSV文件。這將用於通過單獨的Powershell腳本部署多個虛擬機。處理一個單元格中的多個值以導出爲CSV
目前,Export to CSV操作正在處理每個單元的單個VM名稱以及相應的設置,但是在腳本的實際執行中,我們可能會在單個單元中有多個VM名稱(用逗號分隔),但會正在進行的單元格中的設置相同。
我想知道的是,是否有可能/如何去使那些由逗號分隔的單元格中的值填充單獨的行,但仍然具有CSV文件中相應標題下的設置。
代碼:
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
I.E. 如果單元格C:27具有「VM1,VM2,VM3」,則將不同行上的每個值分開,但仍然輸入與其他標題相關聯的值。
目前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腳本以其目前的格式。我希望得到一些幫助,以便我能夠實現預期的結果。
謝謝你的任何援助,您可以提供
嗨,看看在[Split函數(https://msdn.microsoft.com/en-us/library/6x627e5f(V = VS.90 ).aspx) - *「返回一個包含指定數量的子串的基於零的一維數組。」*。您可以將其應用於C27,然後檢查它返回的多少個子字符串。相應地修改你的宏。 –
您還應該查看InStr以首先檢查逗號,並創建用於找到或未找到的邏輯:https://msdn.microsoft.com/en-us/library/8460tsh1(v=vs.90)。 aspx – Sorceri
FWIW - 您的'BuildNullStrings(5)'可以替換爲'String(5,「,」)',擺脫功能的需要。 (或者,如果你願意,改變'BuildNullStrings'的內容來簡單地說'BuildNullStrings = String(numNullStrings,「,」)') – YowE3K