I am using a VBA script to compile data from an Excel document into a CSV file for use with a PowerCLI script. I am encountering issues related to having extra blank spaces in the column names and I would like to either determine a way to:
introduce a function to eliminate the extra spaces in my main VBA script or have the VBA TrimEText section relating to removing the extra spaces run simultaneously with the main VBA script
The 'main' VBA script is:
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 & "Name" & " , "
    logSTR = logSTR & "VLAN" & " , "
    logSTR = logSTR & "NumCPU" & " , "
    logSTR = logSTR & "MemoryGB" & " , "
    logSTR = logSTR & "C" & " , "
    logSTR = logSTR & "D" & " , "
    logSTR = logSTR & "App" & " , "
    logSTR = logSTR & Chr(13)
    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22")
    logSTR = logSTR & BuildNullStrings(1)
    logSTR = logSTR & BuildValuesString("C", "26,27,28,29,30,31,32")
    logSTR = logSTR & Chr(13)
    logSTR = logSTR & BuildNullStrings(6)
    logSTR = logSTR & BuildValuesString("C", "36,37,38,39,40,41,42")
    logSTR = logSTR & Chr(13)
    logSTR = logSTR & BuildNullStrings(6)
    logSTR = logSTR & BuildValuesString("C", "46,47,48,49,50,51,52")
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
The section relating to remove the excess spaces is:
Sub TrimEText()
' This module will trim extra spaces from BOTH SIDES and excessive spaces from inside the text.
    Dim MyCell As Range
    On Error Resume Next
        Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select
        For Each MyCell In Selection.Cells
            MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), " ", " ")
            MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), "", " ")
            MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), "", " ")
            MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), "", " ")
            MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), "", " ")
        Next
    On Error GoTo 0
End Sub
Is there a way to have both run simultaneously without the need for selecting the cells in the exported CSV document? Like specify the cells within the CSV document itself in the VBA script?
