excel - Macro will only create hyperlink in debug mode -
this macro search cells in worksheets in of workbooks contained in single directory. works advertised except add hyperlink method, work if repeatedly mash f8.
how can edit macro hyperlink portion works?
'search workbooks in folder string sub searchworkbooks() dim fso object dim fld object dim strsearch string dim strpath string dim strfile string dim wout worksheet dim wbk workbook dim wks worksheet dim lrow long dim rfound range dim strfirstaddress string on error goto errhandler application.screenupdating = false strsearch = "capacitor" strpath = "c:\!source" set wout = worksheets.add lrow = 1 wout .name = "results" .cells(lrow, 1) = "workbook" .cells(lrow, 2) = "worksheet" .cells(lrow, 3) = "cell" .cells(lrow, 4) = "text in cell" .cells(lrow, 5) = "link" set fso = createobject("scripting.filesystemobject") set fld = fso.getfolder(strpath) strfile = dir(strpath & "\*.xls*") while strfile <> "" set wbk = workbooks.open _ (filename:=strpath & "\" & strfile, _ updatelinks:=0, _ readonly:=true, _ addtomru:=false) each wks in wbk.worksheets set rfound = wks.usedrange.find(strsearch) if not rfound nothing strfirstaddress = rfound.address end if if rfound nothing exit else lrow = lrow + 1 .cells(lrow, 1) = wbk.name .cells(lrow, 2) = wks.name .cells(lrow, 3) = rfound.address .cells(lrow, 4) = rfound.value 'this line not work 'well works in debug mode not in real time wks.hyperlinks.add anchor:=cells(lrow, 5), address:=wbk.fullname, subaddress:= _ wks.name & "!" & rfound.address, texttodisplay:="link" end if set rfound = wks.cells.findnext(after:=rfound) loop while strfirstaddress <> rfound.address next wbk.close (false) strfile = dir loop .columns("a:d").entirecolumn.autofit end 'msgbox "done" exithandler: set wout = nothing set wks = nothing set wbk = nothing set fld = nothing set fso = nothing application.screenupdating = true exit sub errhandler: msgbox err.description, vbexclamation resume exithandler end sub
try adding worksheet reference cells() call out wks.cells(......)
Comments
Post a Comment