excel - Avoid empty cells on CSV export for varying row lenght -


my rows not have same lenght , need avoid "blanks" in between when export csv. example, when export this

1 2 3 4 5

1 2

1 3 3 4

1 2 3 4 5

i this:

1,2,3,4,5

1,2,,,

1,3,3,4,

1,2,3,4,5

and need remove seperators empty cells between. running macro export csv, best if "delete" empty cells in beginning of this.

this small macro will:

  • avoid creating empty csv records corresponding empty excel rows
  • avoid trailing commas


option explicit  sub csv_makerr()    dim r range    dim sout string, k long, m long    dim n long, nfirstrow long, nlastrow long    dim myfilepath string, myfilename string    dim fs, a, mm long    dim separator string     activesheet.usedrange    set r = activesheet.usedrange    nlastrow = r.rows.count + r.row - 1    nfirstrow = r.row    separator = ","     myfilepath = "c:\testfolder\"    myfilename = "whatever"    set fs = createobject("scripting.filesystemobject")    set = fs.createtextfile(myfilepath & myfilename & ".csv", true)     n = nfirstrow nlastrow        k = application.worksheetfunction.counta(cells(n, 1).entirerow)        sout = ""        if k = 0         else            m = cells(n, columns.count).end(xltoleft).column            mm = 1 m                sout = sout & cells(n, mm).text & separator            next mm            sout = left(sout, len(sout) - 1)            a.writeline (sout)        end if    next     a.close end sub 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -