Excel 365桌面版宏编辑问题:运单追踪号转可点击链接
问题与解决:Excel宏适配UPS/FedEx追踪号及代码找回
问题背景
几年前我在Excel中创建了名为HyperAdd的宏,可将UPS追踪号(如1Z2976592400929202)转换为可点击链接:http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=1Z2976592400929202
该宏多年来每周使用均正常,但服务商新增FedEx作为美国本土外地区的物流选项,因此需要修改宏,使其根据追踪号格式自动生成对应物流商的追踪链接。
但在最新版Excel 365中编辑宏时,我在VBA编辑器里找不到原有代码,已遍查编辑器及相关设置仍无果,请问还能在哪里查找?
解决过程与最终代码
在@rotabor的帮助下成功找回代码,最终适配UPS和FedEx的宏代码如下:
Sub HyperAdd() ' 将选中的每个追踪ID转换为可点击的超链接 ' 根据ID格式区分FedEx和UPS物流商 ' 不会修改不包含有效追踪ID的单元格 ' ' 使用方法: ' 1) 选中包含追踪ID的单元格或列 ' 2) 按下<Alt>+<F8> ' 3) 在宏名称面板中选择HyperAdd ' 4) 点击右侧的<运行>按钮 Dim CurrentLink As String Dim CurrentTip As String Dim ValidTrackingID As Boolean FedExLink = "https://www.fedex.com/fedextrack/?trknbr=" FedExTip = "点击查看FedEx包裹最新追踪信息:" UPSLink = "http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=" UPSTip = "点击查看UPS包裹最新追踪信息:" For Each xCell In Selection ValidTrackingID = False ' FedEx追踪号(12位数字格式) If xCell.Value Like "############" Then ValidTrackingID = True CurrentLink = FedExLink & xCell.Value CurrentTip = FedExTip & xCell.Value ' UPS追踪号(以1Z开头,后跟16位字符格式) ElseIf xCell.Value Like "1Z################" Then ValidTrackingID = True CurrentLink = UPSLink & xCell.Value CurrentTip = UPSTip & xCell.Value End If ' 仅修改包含有效追踪ID的单元格 If ValidTrackingID = True Then ' 插入可点击链接 ActiveSheet.Hyperlinks.Add Anchor:=xCell, _ Address:=CurrentLink, _ ScreenTip:=CurrentTip, _ TextToDisplay:=xCell.Value ' 将字体恢复为Ariel xCell.Font.Name = "Ariel" End If Next xCell End Sub
内容的提问来源于stack exchange,提问作者Michael Sheaver
相关产品推荐
相关产品推荐

