Sunday, 15 February 2015

excel - VBA: Exit For Loop when row is empty -



excel - VBA: Exit For Loop when row is empty -

i'm using next code export rows individual text files:

sub export_test() dim firstrow integer, lastrow integer, filename string dim myrow integer, mystr string firstrow = 10 lastrow = 29 myrow = firstrow lastrow filename = "c:\mallet\test\" & cells(myrow, 1) & ".txt" open filename append #1 mystr = cells(myrow, 2).value print #1, mystr close #1 next end sub

the problem code specific number of rows. want utilize code different info samples, number of rows in excel file vary , number in thousands. need lastrow variable set infinite number , exit loop when hits empty row.

this code start in row 10 , run until finds blank cell in sec column. note shortened code bit (though still same writing file):

sub export_test() dim myrow long myrow = 10 while cells(myrow, 2).value <> "" open "c:\mallet\test\" & cells(myrow, 1) & ".txt" append #1 print #1, cells(myrow, 2).value close #1 myrow = myrow + 1 wend end sub

excel vba excel-vba

No comments:

Post a Comment