2012-08-09 89 views
6

在以下子代码中,我想限制它的作用(将超链接中的子字符串替换为)到特定列。我写了'*我的想法是快速修复。但我似乎无法找到一个很好的方式来获取一个单元格的列值保存为一个范围变量。获取范围的列索引

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 

回答

11

您可以直接拨打Column属性:

If MyCell.Column = 1 Then ... 

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

如果你想检查它是否在范围的第一列,可以先计算出它:

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

+ 1是的...祝贺上20K),表示这个问题 – 2012-08-09 12:11:36

+0

正确的答案,但在具体情况下没有帮助我。但我可能找到了另一种解决方法。 Thx的帮助:) – 2012-08-09 12:14:47

+0

@SiddharthRout Yeaa谢谢!你并不遥远;-) – assylias 2012-08-09 12:47:14