Export column with with additional text to Notepad++ for INI File

85 Views Asked by At

I'm trying to create a formatted INI file using Excel.

Let's say I have a column of IP addresses. I'd like the column exported to an INI file with text before and after the IP address.

My INI file should look something like:

[Transfer-List]
Status=Inactive
CountStation=255
Station1=XXX.XXX.XXX.XXX;VKDGenerator
Station2=YYY.YYY.YYY.YYY;VKDGenerator
Station3=ZZZ.ZZZ.ZZZ.ZZZ;VKDGenerator
Station4=AAA.AAA.AAA.AAA;VKDGenerator
Station5=...
...
...

Since I'd like to create an INI file, the first three lines, the Station n° and VKDGenerator are mandatory.

The code below runs, except my ini file is still empty. Notepad result

I think it is due to the formulas in my cells to show the filtered results.
enter image description here

The cells contain the following formula:

 =IFERROR(INDEX(_DATA!$B$2:$F$2685;_DATA!$I15;COLUMN(Generator!$K$3:K16));"")
2

There are 2 best solutions below

3
CDP1802 On

Open a file for output then scan down the spreadsheet printing each line.

Option Explicit

Sub CreateINI()

    Const SUFFIX = ";VKDGenerator"
    Const ININAME = "Filename.INI"
    Const ROW_START = "3"
    Const COL_IP = "K"
    
    Dim cel As Range, rngIP As Range
    Dim n As Long, i As Long, ini As String, ff
    
    'open text file
    ini = ThisWorkbook.Path & Application.PathSeparator & ININAME
    ff = FreeFile
    Open ini For Output As #ff
    
    With ThisWorkbook.Sheets("Sheet1")
        ' number of lines on sheet
        n = .Cells(.Rows.Count, COL_IP).End(xlUp).Row
        If n < ROW_START Then
            MsgBox "No data in column " & COL_IP, vbCritical
        End If
               
        ' visible rows
        Set rngIP = .Cells(ROW_START, COL_IP)
        If n > ROW_START Then
            Set rngIP = rngIP.Resize(n - ROW_START + 1, 1).SpecialCells(xlVisible)
        End If
        n = rngIP.Cells.Count
        
        ' header
        Print #ff, "[Transfer-List]" & vbCrLf & _
                   "Status=Inactive" & vbCrLf & _
                   "CountStation=" & n
        ' stations
        For Each cel In rngIP
            i = i + 1
            Print #ff, "Station" & i & "=" & cel.Text & SUFFIX
        Next
    End With
    Close #ff
    MsgBox n + 3 & " lines written to " & ini, vbInformation
    
    ' open notepad++
    Call Shell("C:\Program Files\Notepad++\notepad++.exe " & ini, 1)
   
End Sub
0
Trendcutter On

Found the solution in the following code:

    Sub ExportToIniFile()
    Dim lastRow As Long
    Dim iniContent As String
    Dim i As Long
    
    ' Find the last row in column K starting from row 3
    lastRow = Cells(Rows.Count, "K").End(xlUp).Row
    
    ' Initialize the INI file content
    iniContent = "[Transfer-List]" & vbCrLf & "Status=Inactive" & vbCrLf
    
    ' Loop through column K starting from row 3 and add stations to the INI content
    For i = 3 To lastRow
        iniContent = iniContent & "Station" & (i - 2) & "=" & Cells(i, 11).Value & ";VKDGenerator" & vbCrLf
    Next i
    
    ' Save the INI file
    SaveIniFile iniContent
End Sub

Sub SaveIniFile(content As String)
    Dim filePath As String
    
    ' Specify the file path where you want to save the INI file
    filePath = "C:\Users\PBurri\Documents\Miniinst.ini"
    
    ' Open a text file for writing
    Open filePath For Output As #1
    
    ' Write the content to the file
    Print #1, content
    
    ' Close the file
    Close #1
End Sub

Thanks to CDP1802 for the pointers and the code. I do now have the .ini file like I wanted it.

I had issues with ThisWorkbook.Path not wanting to write on the OneDrive and for reasons I can't explain, with the Copy and Paste.