How to refactor comparison loops in my excel vba code for speed? -
i new excel vba , not have experience. have 2 worksheets of data compare , if value matches copy , paste second worksheet. use loops compare every row , wondering if there better way this? using brute force , hoping there way program not run long. (i repeat block of code on different sheets 13 times). code consolidating information if meets conditions. below code.
sub consolidate(z) sheets(z).range("b1:axh100").delete '''deletes former values''' = 1 30 x = 1 500 if isempty(sheets("sheet1").cells(x, 13)) 'if cell value empty skip it' = 1 else: if sheets("sheet1").cells(x, 18) = sheets(z).cells(1, 1) 'check see if value same' if sheets("sheet1").cells(x, 13) = sheets(z).cells(i, 1) 'check see if value same' sheets("sheet1").cells(x, 15).copy 'copy value' sheets(z).select 'select second sheet' cells(i, 1).select activesheet.cells(activecell.row, columns.count).end(xltoleft).offset(0, 1).select 'offsets cell left' selection.pastespecial past:=xlpastevalues 'pastes' end sub
as answered here, if need access or change several cells, you're better off reading cells array, working directly array(s) , sending results excel once you're done. it's quite bit more work, , excel's array-handling not best, speed-wise there's no comparison.
this article explains process in more detail.
Comments
Post a Comment