vba - Improve Excel macro that runs slowly -
i have macro makes comparisons , macro exports of changes based on if information doesn't match. have each column gets own worksheet in new workbook. using 7 different counting integers , takes long time because exporting on 60k rows.
question: there faster way execute code? can udf used? if how?
sub export_updates() dim ws worksheet dim wb2 workbook set wb = application.workbooks("total database update_working.xlsm") set ws = wb.worksheets("results") set wb2 = application.workbooks.open("c:\import update.xlsx") = 2 ii = 2 iii = 2 iiii = 2 iiiii = 2 iiiiii = 2 iiiiii = 2 k = 2 wb2.activate while ws.cells(k, 1) <> "" if ws.cells(k, 4) = "no match" wb2.worksheets("ad update").cells(i, 1) = ws.cells(k, 1) wb2.worksheets("ad update").cells(i, 2) = ws.cells(k, 2) = + 1 end if if ws.cells(k, 7) = "no match" wb2.worksheets("senior update").cells(ii, 1) = ws.cells(k, 1) wb2.worksheets("senior update").cells(ii, 2) = ws.cells(k, 5) ii = ii + 1 end if if ws.cells(k, 10) = "no match" wb2.worksheets("id update").cells(iii, 1) = ws.cells(k, 1) wb2.worksheets("id update").cells(iii, 2) = ws.cells(k, 8) iii = iii + 1 end if if ws.cells(k, 13) = "no match" wb2.worksheets("minor update").cells(iiii, 1) = ws.cells(k, 1) wb2.worksheets("minor update").cells(iiii, 2) = ws.cells(k, 11) end if if ws.cells(k, 16) = "no match" wb2.worksheets("major update").cells(iiii, 1) = ws.cells(k, 1) wb2.worksheets("major update").cells(iiii, 2) = ws.cells(k, 14) iiii = iiii + 1 end if if ws.cells(k, 19) = "no match" wb2.worksheets("cap update").cells(iiiii, 1) = ws.cells(k, 1) wb2.worksheets("cap update").cells(iiiii, 2) = ws.cells(k, 17) iiiii = iiiii + 1 end if if ws.cells(k, 22) = "no match" wb2.worksheets("pl update").cells(iiiiii, 1) = ws.cells(k, 1) wb2.worksheets("pl update").cells(iiiiii, 2) = ws.cells(k, 20) iiiiii = iiiiii + 1 end if k = k + 1 loop wb2.save sleep (1000) wb2.close savechanges:=true wb.activate end sub
any suggestions welcome.
i'd suggest using vba arrays processing. going between excel environment , vba environment s-l-o-w process, , doing multiple times each row of data. there little more programming involved using vba arrays, speed difference can significant (32x fast sample of 60k rows).
in general, best practice
- read data excel vba array in 1 step, using 2-d array
- process data in vba, storing results in vba array
- at end, transfer vba array excel
here example workbook showing more specifics.
and here faster vba code:
sub method2() dim ws worksheet dim wsoutput worksheet dim rngrawdata range dim rngoutput range dim rngtodelete range dim varawdata() variant dim vadiffs() variant t = timer set ws = activesheet set rngrawdata = ws.range("a1").currentregion ' transfers excel data vba array in 1 step. varawdata = rngrawdata 'loop through vba array, adding no match entries diffs array redim vadiffs(rngrawdata.rows.count, 1 3) idiffs = 0 = lbound(varawdata, 1) ubound(varawdata, 1) if varawdata(i, 4) = "no match" idiffs = idiffs + 1 vadiffs(idiffs, 1) = varawdata(i, 1) ' capture id vadiffs(idiffs, 2) = varawdata(i, 2) ' capture source1 value vadiffs(idiffs, 3) = varawdata(i, 3) ' capture source 2 value end if next 'transfer diffs array excel set wsoutput = worksheets("diff2") wsoutput.range("a1") = vadiffs 'delete rows wsoutput.cells(idiffs + 2, 1) = "end" set rngtodelete = wsoutput.range(wsoutput.cells(idiffs + 2, 1), _ wsoutput.cells(rngrawdata.rows.count + 3, 1)) rngtodelete.entirerow.delete wsoutput.activate msgbox "it took " & timer - t & " seconds." end sub
Comments
Post a Comment