W
W
WebforSelf2021-04-02 12:58:43
Visual Basic
WebforSelf, 2021-04-02 12:58:43

How to convert csv using a macro?

There is such a file in csv in which there are 3 columns of data.
How to use a macro to convert it into a price list like this one on the screenshot.
That is,
1- combine the name of the first column, and combine all its data into 2 and 3 through | considering empty.

6066ea2f98372137283142.png

This macro does almost what you need, only it does not combine the first field into one.

Sub SaveAsCSVinQuotes()
Dim r As Range, c As Range, s As String
s = Application.GetSaveAsFilename(, "CSV Files (*.csv),*.csv,All Files (*.*),*.*", , "CSV ? |")
If s = "False" Then Exit Sub
Open s For Output As #1
   s = ""
For Each r In ActiveSheet.UsedRange.columns
   s_row = ""
   For Each c In r.Cells
        s_row = s_row & c & "|"
   Next
  If s = "" Then
    s = s_row
  Else
    s = s & ";" & s_row
  End If
Next
Print #1, s
Close #1
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VitalyChaikin, 2021-04-02
@VitalyChaikin

See an example of working with the file
https://excelvba.ru/code/TextFile2Array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question