hyperlink in visual basic excel -
what want in code when click cell b3 in sheet1 direct me a5 in sheet2 vice versa, when click a5 in sheet2 bring me b3 in sheet1 http://i.stack.imgur.com/qungd.jpg
sub macro3() ' ' macro3 macro ' ' range("b3").select activesheet.hyperlinks.add anchor:=selection, address:="", subaddress:= _ "sheet2!a5", texttodisplay:="gg" sheets("sheet2").select activesheet.hyperlinks.add anchor:=selection, address:="", subaddress:= _ "sheet1!b3", texttodisplay:="gg" sheets("sheet1").select end sub
now problem if name of sheet userdefined? there's messagebox says should input name of sheet. sub address changes not "sheet2" anymore. instance put in msgbox "123", name of sheet become "sheet2 123".
you should take advantage of indexing of worksheets collection.
sub indexingsheets() sheets(1).range("b3").formula = _ "=hyperlink(""#" & thisworkbook.sheets(2).name & "!a5"", ""texttodisplay"")" sheets(2).range("a5").formula = _ "=hyperlink(""#" & thisworkbook.sheets(1).name & "!b3"", ""texttodisplay"")" end sub
this code assumes sheet1 , sheet2 first 2 sheets in workbook.
if wanted ask name use instead:
code checks if sheet exists ( can refer ). if executes macro , if doesn't calls procedure recursively ask name.
dim sheetexist boolean sub prefnamedsheets() dim shname$, i& shname = inputbox("whats second sheet name?") = 1 worksheets.count if strcomp(cstr(sheets(i).name), shname, vbtextcompare) = 0 sheetexist = true end if next if sheetexist activesheet.range("b3").formula = _ "=hyperlink(""#" & shname & "!a5"", ""texttodisplay"")" sheets(shname).range("a5").formula = _ "=hyperlink(""#" & thisworkbook.sheets(1).name & "!b3"", ""texttodisplay"")" else call prefnamedsheets end if end sub
you may find this link useful!
Comments
Post a Comment