经过一番研究,如果发现一篇旧帖子为我提供了答案
Link to original post
您只需将其粘贴到工作表代码而不是模块中!并在代码中更改要生成超长超链接的单元格范围。
如果您像我一样在公式中有一些动态范围,那么您唯一需要做的就是强制刷新公式。我刚刚录制了一个快速宏,通过单击按钮将公式重新输入到单元格中。并且链接会根据所选的日期范围发生相应的变化。
希望它对其他人有用:)
下面的测试代码:
' davesexel http://www.excelforum.com/excel-general/1125569-creating-in-excel-a-hyperlink-to-a-route-made-in-google-maps.html#post4311156
Private Sub Worksheet_Change(ByVal Target As Range)
'Application.EnableEvents = False 'Does not appear to need this.. I thought it would as i thought the change in the sheet done in line 60 would set this code off again. This code line would have avoided that by turning off codes of this nature ***
10 Dim strDaveLongSExelHype As String 'Done at complile time to Tell VBA initially where to go when seeing strDaveLongSexelHype anywhere else in the code ( This may likely change as required to hause changing length values http://www.mrexcel.com/forum/excel-questions/361246-vbnullstring.html
20 Let strDaveLongSExelHype = Target.Value 'Target is a reserved Word used for VBA for the Range Object corresponding to where you "Hit" ( or the Range which you selected before hitting hit Enter ) The .Value Property assigned to one cell returns the value in an appropriate type, ( in our case typically a String ), of what is in the cell. In other words what you typed in is "put in" strDaveLongSExelHype
30 Dim WhereIWantToInsertMyLongThong As Range 'Variable for the Spreadsheet Area, as a Range Object, where you are expecting to Insert your Long Things.
40 Set WhereIWantToInsertMyLongThong = Range("A13:A14") ' CHANGE this to suit your wishes! ####
'41 Set WhereIWantToInsertMyLongThong = Cells ' ##### This alternatve would result in the code working over the whole Worksheet
50 If Not Intersect(WhereIWantToInsertMyLongThong, Target) Is Nothing Then 'The VBA Intersect Method will return a Range Object that represents the intersection of two, or more, ranges. Nothing is a Special VBA Object for a "not yet assigned Object". If we Not get that then we have a Object, ( in this case a range Object ( a Cell ) ) that intercepts the entire Range where you wanted to be able to Insert your Long Thing ( That entire Range was specified in Line 40 ( or 41 ) )
' The Hyperlink Property of a range returns ( or refers ) to a collection of "all to do with" the Hyperlinks associated with that range Object. Amongst other things a .Add Method is available with 2 compulsory and 2 optional arguments. Based on the arguments, a specific Hyperlink is put in the Range. Anchor:= _____ says where the Hyperlinkk is written in. Address:=_____in this case is the URL itself. ScreenTip:=____is the bit of text in the cell which you usually put in as something like “Click Me”. But just for fun I used the whole text of your URL again. TextToDisplay:=___Is where you put what you want to be displayed in the Box which comes up when you hover over your Hyperlink cell with the mouse. Again just for fun I used your whole URL. I guess you might want just something Like “This goes to my Google Maps Pub Crawl Route when I click on it”
60 Target.Hyperlinks.Add Anchor:=Target, Address:="" & strDaveLongSExelHype & "", ScreenTip:="" & strDaveLongSExelHype & "", TextToDisplay:="" & strDaveLongSExelHype & "" ' " DaveSexuel's Workaround " Allowing a very long ( greater than 255 character ) URL to be put in an Excel Cell http://www.excelforum.com/excel-general/1125569-creating-in-excel-a-hyperlink-to-a-route-made-in-google-maps.html#post4311156
'70 Else 'Do Not need to do anything here... but just for fun, lines 70 and 75 would result in a message box poping up telling you that you selected outside the range you specified in Line 40 ( or 41 )
'75 MsgBox prompt:="Your Selected Range was not within Area " & Replace((WhereIWantToInsertMyLongThong.Address), "$", "", 1) & "" ' Address Property applied to Range Object returns a String including the $ bits. So a simple Replace withn that String , of a $ , with a special VBA String representing a string variable not filled yet , starting looking at character 1 ( first ) , and replace all of them ( that is what -1 means in this case )
80 End If
Application.EnableEvents = True ' I leave this in just to be on the safe side!! **
End Sub