Speed Up Working With Comments in Excel VBA -
this example contrived, created explain problem i'm having. want code run faster does. on new sheet each loop of cell starts fast, if let run near completion, , run again, hit 100ms per cell. in sheet have 16000 cells lot of comments this, , manipulated individually every time code runs. in example same, in real application each 1 different.
is there anyway make process faster?
option explicit public declare ptrsafe function gettickcount lib "kernel32.dll" () long public sub breakthecommentsystem() dim integer dim t long dim cell range dim dr range set dr = range(cells(2, 1), cells(4000, 8)) dim rstr string rstr = "abcdefg hijk lmnop qrs tuv wx yz" & chr(10) = 1 5 rstr = rstr & rstr next each cell in dr t = gettickcount cell if .comment nothing .addcomment else .comment .shape.textframe.characters.font .bold = true .name = "arial" .size = 8 end .shape.textframe.autosize = true .text rstr end end if end debug.print (gettickcount - t & " ms ") next rstr = empty = empty t = empty set cell = nothing set dr = nothing end sub
update 12-11-2015, wanted noted somewhere in case runs it, reason trying optimize because vsto not let me add workbook file these comments. after 6 months of working microsoft, confirmed bug in vsto , excel.
according msdn comments collection , comment object documentation, can reference comments within worksheet through indexed position , deal them directly rather cycle through each cell , determine whether contains comment.
dim c long activesheet '<- set worksheet reference properly! c = 1 .comments.count .comments(c) debug.print .parent.address(0, 0) ' .parent cell containing comment ' stuff .comment object end next c end
also according official docs range.specialcells method can determine subset of cells in worksheet using xlcelltypecomments constant type parameter.
dim comcel range activesheet '<- set worksheet reference properly! each comcel in .cells.specialcells(xlcelltypecomments) comcel.comment debug.print .parent.address(0, 0) ' .parent cell containing comment ' stuff .comment object end next comcel end
i'm still unclear reasoning behind filling non-commented cells blank comment if trying work comments on worksheet better work subset of commented cells rather cycling through cells looking comment.
Comments
Post a Comment