列名称中的Excel列号

Par*_*uja 65 excel vba excel-vba

如何使用Excel宏从Excel中的列名获取列号?

Sid*_*out 152

我想你想要这个?

列名称到列号

Sub Sample()
    ColName = "C"
    Debug.Print Range(ColName & 1).Column
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑:还包括你想要的反向

列号到列名称

Sub Sample()
    ColNo = 3
    Debug.Print Split(Cells(, ColNo).Address, "$")(1)
End Sub
Run Code Online (Sandbox Code Playgroud)

跟进

就像我在最顶端的工资字段让我们说在单元格C(1,1)现在,如果我改变文件并将工资列转移到其他地方说F(1,1)那么我将不得不修改代码所以我希望代码检查Salary并找到列号,然后根据该列号执行其余操作.

在这种情况下,我建议使用.FIND请参阅下面的示例

Option Explicit

Sub Sample()
    Dim strSearch As String
    Dim aCell As Range

    strSearch = "Salary"

    Set aCell = Sheet1.Rows(1).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

    If Not aCell Is Nothing Then
        MsgBox "Value Found in Cell " & aCell.Address & _
        " and the Cell Column Number is " & aCell.Column
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

快照

在此输入图像描述