Create excel ranges using column numbers in vba?

前端 未结 9 2123
误落风尘
误落风尘 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:09

    In case you were looking to transform your column number into a letter:

    Function ConvertToLetter(iCol As Integer) As String
        Dim iAlpha As Integer
        Dim iRemainder As Integer
        iAlpha = Int(iCol / 27)
        iRemainder = iCol - (iAlpha * 26)
        If iAlpha > 0 Then
            ConvertToLetter = Chr(iAlpha + 64)
        End If
        If iRemainder > 0 Then
            ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
        End If
    End Function
    

    This way you could do something like this:

    Function selectColumnRange(colNum As Integer, targetWorksheet As Worksheet)
        Dim colLetter As String
        Dim testRange As Range
        colLetter = ConvertToLetter(colNum)
        testRange = targetWorksheet.Range(colLetter & ":" & colLetter).Select
    End Function
    

    That example function would select the entire column ( i.e. Range("A:A").Select)

    Source: http://support.microsoft.com/kb/833402

提交回复
热议问题