vba - 获取范围的列索引

标签 vba excel

在下面的子代码中,我想将其功能(替换超链接中的子字符串)限制为特定列。我已经在 '* 中写下了我的快速修复想法。但我似乎找不到获取保存为范围变量的单元格的列值的好方法。

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/

相关文章:

oracle - 多个 Oracle Smartview 刷新不在循环中工作(当我单步执行代码时有效)

java - 使用 Apache POI 3.14 获取 Excel 中公式的计算值

来自单个单元格的 Excel 数据验证列表

arrays - 几个不连续的列到数组中

vba - 如何从用户表单打开文件并在用户表单打开的情况下对其进行修改

Excel/VBA : Variable Value/Text into concatenate formula

excel - 如何匹配二维数组的第一列

excel - 检查字符串是否以特定字符结尾并将其删除 - 错误 "invalid identifier"

c# - 使用 ASP.NET 自动化 Excel

vba - 以编程方式重命名 activeX 文本框 "(name)"字段