Excel 如何用VBA得到第几列的列号
2个回答
展开全部
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 获取列名 2019-01-01
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Function GetColName(ByVal colN As Long) As String
Dim tryBase As Long, tryPow As Long, colC As Long
Dim colNameTbl As String, tempColN As String
'colNameTbl = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
' 数值异常,按第一列输出
If colN <= 0 Then
GetColName = "A"
Exit Function
End If
'colName = Split(colNameTbl, ",")
' 列名编号和取值范围
' A- Z: 1-26
' AA- ZZ: 26+(1-26*26)
' AAA-ZZZ: 26+26*26+(1-26*26*26)
' ...
' 实际上最新的EXCEL只能支持16384列:XFD,但程序可以触及到任何 long 值上限
' 确定需要几个字母来表示列号
tryBase = 0: tryPow = 1: colC = colN - 1
Do While tryBase + tryPow * 26 <= colC
tryBase = tryBase + tryPow * 26
tryPow = tryPow * 26
Loop
' 根据字母数生成列名
colC = colC - tryBase: tempColN = ""
Do While tryPow > 0
tempColN = Chr(65 + (colC Mod 26)) & tempColN
colC = Int(colC / 26): tryPow = Int(tryPow / 26)
Loop
GetColName = tempColN
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 获取列号 2019-01-03
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Function GetColNum(ByVal colName As String) As Long
Dim tryBase As Long, tryPow As Long, colNum As Long
Dim colNameTbl As String, tempColN As String
' 数值异常,按第一列输出
If colName = "" Or colName Like "*[!a-zA-Z]*" Then
GetColNum = 1
Exit Function
End If
colName = UCase(colName)
' 列名编号和取值范围
' A- Z: 1-26
' AA- ZZ: 26+(1-26*26)
' AAA-ZZZ: 26+26*26+(1-26*26*26)
' ...
' 实际上最新的EXCEL只能支持16384列:XFD,但程序可以触及到任何 long 值上限
' 确定编号的base值
tryBase = 0: tryPow = 1
For i = 1 To Len(colName) - 1
tryBase = tryBase + tryPow * 26
tryPow = tryPow * 26
Next i
' 按序号求最终行号
tryPow = 1: colNum = 1
For i = Len(colName) To 1 Step -1
colNum = colNum + (Asc(Mid(colName, i, 1)) - 65) * tryPow
tryPow = tryPow * 26
Next i
GetColNum = colNum + tryBase
End Function
' 获取列名 2019-01-01
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Function GetColName(ByVal colN As Long) As String
Dim tryBase As Long, tryPow As Long, colC As Long
Dim colNameTbl As String, tempColN As String
'colNameTbl = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
' 数值异常,按第一列输出
If colN <= 0 Then
GetColName = "A"
Exit Function
End If
'colName = Split(colNameTbl, ",")
' 列名编号和取值范围
' A- Z: 1-26
' AA- ZZ: 26+(1-26*26)
' AAA-ZZZ: 26+26*26+(1-26*26*26)
' ...
' 实际上最新的EXCEL只能支持16384列:XFD,但程序可以触及到任何 long 值上限
' 确定需要几个字母来表示列号
tryBase = 0: tryPow = 1: colC = colN - 1
Do While tryBase + tryPow * 26 <= colC
tryBase = tryBase + tryPow * 26
tryPow = tryPow * 26
Loop
' 根据字母数生成列名
colC = colC - tryBase: tempColN = ""
Do While tryPow > 0
tempColN = Chr(65 + (colC Mod 26)) & tempColN
colC = Int(colC / 26): tryPow = Int(tryPow / 26)
Loop
GetColName = tempColN
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 获取列号 2019-01-03
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Function GetColNum(ByVal colName As String) As Long
Dim tryBase As Long, tryPow As Long, colNum As Long
Dim colNameTbl As String, tempColN As String
' 数值异常,按第一列输出
If colName = "" Or colName Like "*[!a-zA-Z]*" Then
GetColNum = 1
Exit Function
End If
colName = UCase(colName)
' 列名编号和取值范围
' A- Z: 1-26
' AA- ZZ: 26+(1-26*26)
' AAA-ZZZ: 26+26*26+(1-26*26*26)
' ...
' 实际上最新的EXCEL只能支持16384列:XFD,但程序可以触及到任何 long 值上限
' 确定编号的base值
tryBase = 0: tryPow = 1
For i = 1 To Len(colName) - 1
tryBase = tryBase + tryPow * 26
tryPow = tryPow * 26
Next i
' 按序号求最终行号
tryPow = 1: colNum = 1
For i = Len(colName) To 1 Step -1
colNum = colNum + (Asc(Mid(colName, i, 1)) - 65) * tryPow
tryPow = tryPow * 26
Next i
GetColNum = colNum + tryBase
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询