获取范围的列索引

9
在以下的子代码中,我想限制它所做的事情(替换超链接中的子字符串)只在特定的列中进行。我已经在“*”处写下了我快速解决的想法,但是我似乎找不到一种好的方法来获取保存为Range变量的单元格的列值。
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
1个回答

16
您可以直接调用Column属性:
If MyCell.Column = 1 Then ...

这是绝对列(电子表格的A列),而不是范围中的第一列。

如果您想检查它是否为范围的第一列,可以先进行计算:

firstCol = yourRange.Cells(1, 1).Column
If MyCell.Column = firstCol Then ...

回答问题是正确的,但对我的特定情况没有帮助。但我可能已经找到了另一种解决方法。感谢您的帮助 :) - Øyvind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接