excel - Loop through all the files in the current directory -


my macro requires me physically open comma delimited file, run macro, save file repeat.

i require macro go through files in current directory make changes save itself.

the file directory is
x:\new gas team 2016\incorrect ttz database\read flows\umr

after conversion file needs saved following
x:\new gas team 2016\incorrect ttz database\read flows\umr\converted

as excel macro enabled workbook file name same before conversion.

sub umr() ' ' umr macro ' range("a1").select activecell.formular1c1 = "transaction_type" range("b1").select activecell.formular1c1 = "meter_point_ref" range("c1").select activecell.formular1c1 = "actual_read_date" range("d1").select activecell.formular1c1 = "meter_reading_source" range("e1").select activecell.formular1c1 = "meter_reading_reason" range("f1").select activecell.formular1c1 = "meter_serial_number" range("g1").select activecell.formular1c1 = "meter_reading" range("h1").select activecell.formular1c1 = "meter_roc_count" range("i1").select activecell.formular1c1 = "meter_read_verified" range("j1").select activecell.formular1c1 = "corrector_serialnumber" range("j1").select activecell.formular1c1 = "corrector_serial_number" range("k1").select activecell.formular1c1 = "corrector_uncorrected_reading" range("l1").select activecell.formular1c1 = "corrector_corrected_reading" range("m1").select activecell.formular1c1 = "corrector_roc_count" range("n1").select activecell.formular1c1 = "corrector_usable_ind" range("o1").select activecell.formular1c1 = "corrector_read_verified"  columns("c:c").columnwidth = 8.29 columns("c:c").entirecolumn.autofit columns("b:b").entirecolumn.autofit columns("a:a").entirecolumn.autofit columns("e:e").select columns("d:d").entirecolumn.autofit columns("e:e").entirecolumn.autofit  columns("f:f").entirecolumn.autofit columns("g:g").entirecolumn.autofit columns("h:h").entirecolumn.autofit activewindow.scrollcolumn = 2 activewindow.scrollcolumn = 3 columns("i:i").entirecolumn.autofit columns("j:j").entirecolumn.autofit range("q1").select columns("k:k").entirecolumn.autofit columns("l:l").entirecolumn.autofit range("r1").select columns("m:m").entirecolumn.autofit columns("n:n").entirecolumn.autofit columns("o:o").entirecolumn.autofit call m_z99 end sub  sub m_z99()  'application.screenupdating = false  '  dim acell range  activesheet.range("a2").select  trans_count = cells(rows.count, 1).end(xlup).row  = 0  until = trans_count      = + 1      if activecell.value = "z99"         call delete_row     elseif activecell.value = ""         msgbox "done"     else: call t_skip     end if  loop end sub  sub delete_row() activecell.entirerow.select selection.delete shift:=xlup end sub  sub t_skip() activecell.offset(1, 0).select                                 end sub 

you'd use dir function. here's example of loop you'd nest code in:

'get count of files merged f = dir(fol & "\" & y & "*office hrs.xls*") while len(f) > 0     fc = fc + 1     f = dir      'fail safe escape option     if fc > 600 & len(f) > 0         msgbox "an error has occurred causing appear there more 600 files in specified folder.", vbokonly, "overly large file count"             application.calculation = xlcalculationautomatic             application.screenupdating = true             end     end if loop 

also, should remove select macro, pretty everywhere appears. selects aren't required , slow code down. example:

range("a1").select activecell.formular1c1 = "transaction_type" 

should be

range("a1").value = "transaction_type" 

also, example kind of alludes to, unless you're adding excel formula cell, .value property should setting


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -