2012-08-09 84 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