vba - Excel is freezing when importing large CSV -


i have large csv file, 60 columns , 50k rows. (i can't show because in intranet.)

i wrote vba code this:

columns("d:bf").select selection.clearcontents *code import csv file d1* lastrow = cells(rows.count, "d").end(xlup).row range("a1:c1").select selection.copy range("a2:c" & lastrow).select selection.pastespecial paste:=xlpasteformulas, operation:=xlnone, _     skipblanks:=false, transpose:=false 

it working, has stopped working. when run macro excel freezes. need press "esc" , macro stops, debugger says pastespecial wrong. not, if run macro import, , press "esc", imports well, run second macro paste formulas until last row , runs too.

the workbook in manual calculation

http://i.stack.imgur.com/hkzje.jpg

read csv file line line array 10,000 rows 100 columns. when array full write excel, redim array , continue until end of file.

i tried use dynamic array excel freeze up. using static array more efficient.

you'll need adjust constant values. image looks file semi-colon delimited. matter if columncount exact long greater or equal actual column count.

const csvfilename = "c:\users\best buy\downloads\stackoverfow\sample data file\r58k x c60.csv" const delimiter = "," const pagesize = 10000 const columncount = 100 

it takes 32 seconds import 100,000 rows , 64 columns. csv file 69.5 mb on disk.

enter image description here

option explicit  sub processfile()     const csvfilename = "c:\users\best buy\downloads\stackoverfow\sample data file\r58k x c60.csv"     const delimiter = ","      activesheet.displaypagebreaks = false     application         .screenupdating = false         .calculation = xlcalculationmanual     end      debug.print     dim start: start = timer      dim lastrow long     dim arformulas     arformulas = range("a1:c1").formula      columns("a:bf").clearcontents      importcsvfile csvfilename, delimiter      debug.print "time import csv file in seconds:"; timer - start     start = timer      lastrow = cells(rows.count, "d").end(xlup).row      range("a1:c" & lastrow).formula = arformulas      debug.print "time add formulas in seconds:"; timer - start      debug.print "column count:"; worksheets("sheet1").usedrange.columns.count     debug.print "row count:"; worksheets("sheet1").usedrange.rows.count      application         .screenupdating = true         .calculation = xlcalculationautomatic         .autorecover.enabled = true     end end sub  sub importcsvfile(filepath string, delimiter string)     const pagesize = 10000     const columncount = 100      dim line string     dim ardata, arline     dim x long, y long, z long     redim ardata(pagesize, columncount)     z = 1      open filepath input #1                     ' open file input     while not eof(1)                               ' loop until end of file         line input #1, line         arline = split(line, delimiter)          y = 0         y = 0 ubound(arline)             ardata(x, y) = arline(y)         next         x = x + 1          if x = pagesize or eof(1)              range("d" & z).resize(x, y) = ardata             z = z + x             redim ardata(pagesize, columncount)             x = 0         end if      loop      close #1      erase ardata end sub 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -