请教VBA:如何能输入一个列序号,返回对应的字母
答案:1 悬赏:0 手机版
解决时间 2021-02-19 12:38
- 提问者网友:箛茗
- 2021-02-19 01:08
请教VBA:如何能输入一个列序号,返回对应的字母
最佳答案
- 五星知识达人网友:人類模型
- 2021-02-19 02:09
参考代码:
' #################################
' [功能描述] 通过列索引获得列名称.
' [参数列表] ColumnIndex 列索引.
' [返回类型] Long 列名称.
' #################################
Function GetNameByColumnIndex(ByVal ColumnIndex As Long) As String
Dim intMod As Integer
Do While ColumnIndex > 0
intMod = ColumnIndex Mod 26
If intMod > 0 Then
GetNameByColumnIndex = Chr$(intMod + 64) & GetNameByColumnIndex
Else
GetNameByColumnIndex = "Z" & GetNameByColumnIndex
ColumnIndex = ColumnIndex - 1
End If
ColumnIndex = ColumnIndex \ 26
Loop
End Function
反向扩展:
' ################################
' [功能描述] 通过列名称获得列索引.
' [参数列表] ColumnName 列名称.
' [返回类型] Long 列索引.
' ################################
Function GetIndexByColumnName(ByVal ColumnName As String) As Long
Dim intLoop As Integer
Dim intLength As Integer
intLength = Len(ColumnName)
ColumnName = UCase$(ColumnName)
GetIndexByColumnName = Asc(Mid$(ColumnName, intLength, 1)) - 64
For intLoop = 1 To intLength - 1
GetIndexByColumnName = GetIndexByColumnName + _
(Asc(Mid$(ColumnName, intLoop, 1)) - 64) * 26 ^ _
(intLength - intLoop)
Next
End Function
' #################################
' [功能描述] 通过列索引获得列名称.
' [参数列表] ColumnIndex 列索引.
' [返回类型] Long 列名称.
' #################################
Function GetNameByColumnIndex(ByVal ColumnIndex As Long) As String
Dim intMod As Integer
Do While ColumnIndex > 0
intMod = ColumnIndex Mod 26
If intMod > 0 Then
GetNameByColumnIndex = Chr$(intMod + 64) & GetNameByColumnIndex
Else
GetNameByColumnIndex = "Z" & GetNameByColumnIndex
ColumnIndex = ColumnIndex - 1
End If
ColumnIndex = ColumnIndex \ 26
Loop
End Function
反向扩展:
' ################################
' [功能描述] 通过列名称获得列索引.
' [参数列表] ColumnName 列名称.
' [返回类型] Long 列索引.
' ################################
Function GetIndexByColumnName(ByVal ColumnName As String) As Long
Dim intLoop As Integer
Dim intLength As Integer
intLength = Len(ColumnName)
ColumnName = UCase$(ColumnName)
GetIndexByColumnName = Asc(Mid$(ColumnName, intLength, 1)) - 64
For intLoop = 1 To intLength - 1
GetIndexByColumnName = GetIndexByColumnName + _
(Asc(Mid$(ColumnName, intLoop, 1)) - 64) * 26 ^ _
(intLength - intLoop)
Next
End Function
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯