Create excel ranges using column numbers in vba?

前端 未结 9 2111
误落风尘
误落风尘 2021-02-02 05:56

How is it possible to create a range in vba using the column number, rather than letter?

9条回答
  •  既然无缘
    2021-02-02 06:24

    Here is a condensed replacement for the ConvertToLetter function that in theory should work for all possible positive integers. For example, 1412 produces "BBH" as the result.

    Public Function ColumnNumToStr(ColNum As Integer) As String
    Dim Value As Integer
    Dim Rtn As String
        Rtn = ""
        Value = ColNum - 1
        While Value > 25
            Rtn = Chr(65 + (Value Mod 26)) & Rtn
            Value = Fix(Value / 26) - 1
        Wend
        Rtn = Chr(65 + Value) & Rtn
        ColumnNumToStr = Rtn
    End Function
    

提交回复
热议问题