Wednesday, 15 January 2014

Excel VBA: import csv, keep column names, and convert text to column -



Excel VBA: import csv, keep column names, and convert text to column -

i'm trying adapt macro found online. want take csv files 1 folder, import them excel workbook, utilize file names headers each column, , split each imported column separate columns using commas delimiters.

i don't have much of programing background.

here code (adapted script jerry beaucaire)

sub importmanytxtintocolumns() dim fpath string, ftxt string dim wstrgt worksheet, nc long application.screenupdating = false fpath = "t:\desktop\test\" 'path files, remember final \ set wstrgt = thisworkbook.sheets.add 'new sheet incoming info nc = 1 'first column info ftxt = dir(fpath & "*.txt") 'get first filename while len(ftxt) > 0 'process 1 @ time 'open file in excel workbooks.opentext fpath & ftxt, origin:=437 'put filename in target column wstrgt.cells(1, nc) = activesheet.name 'copy column new sheet range("a:a").specialcells(xlconstants).copy wstrgt.cells(2, nc) wstrgt.range("a16:a2815").select range(selection, selection.end(xldown)).select selection.texttocolumns destination:=wstrgt.range("a16:a2815"), datatype:=xldelimited, _ textqualifier:=xldoublequote, consecutivedelimiter:=false, tab:=true, _ semicolon:=false, comma:=true, space:=false, other:=false, fieldinfo _ :=array(array(1, 1), array(2, 1)), trailingminusnumbers:=true activeworkbook.close false 'close source file nc = nc + 2 'next column ftxt = dir 'next file loop application.screenupdating = true end sub

excel vba excel-vba csv

No comments:

Post a Comment