在下面的子代码中,我想将其功能(替换超链接中的子字符串)限制为特定列。我已经在 '* 中写下了我的快速修复想法。但我似乎找不到获取保存为范围变量的单元格的列值的好方法。
Dim MyDoc As Worksheet
Dim MyCell As Range
...
For Each MyCell In MyDoc.UsedRange
If MyCell.Hyperlinks.Count > 0 Then
'* if mycell's columnnumber = 1 then
LinkURL = MyCell(1).Hyperlinks(1).Address
FindPos = InStr(1, LinkURL, FindString)
If FindPos > 0 Then 'If FindString is found
ReplaceLen = Len(FindString)
URLLen = Len(LinkURL)
PreStr = Mid(LinkURL, 1, FindPos - 1)
PostStr = Mid(LinkURL, FindPos + ReplaceLen, URLLen)
NewURL = PreStr & ReplaceString & PostStr
MyCell(1).Hyperlinks(1).Address = NewURL 'Change the URL
End If
'* End if
End If
Next MyCell
最佳答案
您可以简单地调用Column
属性:
If MyCell.Column = 1 Then ...
这是绝对列(电子表格的 A 列),而不是范围的第一列。
如果你想检查它是否是范围的第一列,你可以先计算它:
firstCol = yourRange.Cells(1, 1).Column
If MyCell.Column = firstCol Then ...
关于vba - 获取范围的列索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11881764/