从第一行的单元格名称中找出 Excel 列字符

Find out Excel column character from name of cell in first row

我有一个 excel sheet 看起来像这样;

header 个名字位于第一行。我想要一个 VBA 函数,它 return 是列号,给定第一行中的单元格名称。例如,getColumnNumber("B Symbol") 应该 return B。 getColumnNumber("C Symbol") 应该 return C.

我正在使用 Excel 2016.

找到文本,然后阅读地址。

With Worksheets(1).Range("a1:z1") ' 
    Set c = .Find("whatever you need to find", lookin:=xlValues) 
    If Not c Is Nothing Then 
        firstAddress = split(c.Address,"$")(0) 
        msgbox firstAddress 
    End If 
End With

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel

您不需要 VBA。下面的公式同样适用,任何 VBA 都使用基本相同的方法。

=SUBSTITUTE(ADDRESS(1, MATCH("C Symbol", 1:1, 0), 4), 1, TEXT(,))

这是我自己的问题的答案,修改自泰迪熊。他的回答有点错误。

With Worksheets(1).Range("a1:z1") ' 
    Set c = .Find("whatever you need to find", lookin:=xlValues) 
    If Not c Is Nothing Then 
        firstAddress = split(c.Address,"$")(1) 'should be 1, not 0 
        msgbox firstAddress 
    End If 
End With