进来学习学习
Function gotcolu(ByVal liename As String) As Integer
'根据字母,返回列序号。
gotcolu = Range(liename & "8").Column
End Function
对不起,刚仔细看了你的需求,我的函数是根据字母,返回数字,而你要的是根据数字,返回列名字,那么我的方向反了。Orz
另外8楼的方法不错,但列序号超过702时会错。
这里完善一下我的代码,都是用VBA做在EXCEL里的函数,可以完善的互相转换:
程序代码:Function GotColNoXLS(ByVal liename As String) As Integer
Dim i As Integer, isLalpha As Boolean
'根据字母,返回列序号。本模块直接利用EXCEL单元格Column属性值,非数学方式计算。
isLalpha = True
If Len(Trim(liename)) = 0 Then '处理空值、空白
GotColNoXLS = 0
Else
'避免用户恶意输入,先确保用户输入的是纯字母,其它字符则直接给出错误提示。
liename = UCase(Trim(liename))
For i = 1 To Len(liename) Step 1
If Not (Asc(Mid(liename, i, 1)) >= 65 And Asc(Mid(liename, i, 1)) <= 90) Then
isLalpha = False
End If
If isLalpha = False Then Exit For
Next i
If isLalpha = False Then
GotColNoXLS = 0
Else
GotColNoXLS = Range(liename & "1").Column
End If
End If
End Function
再来一个根据列序号,返回列字母名称的:
程序代码:Function GetColNameXLS(LXH As Integer) As String
'根据列序号,返回列字母名,本模块利用Excel本身的AddressLocal转换,非数学方式计算。
Dim ad1 As String
'净化LXH,避免用户恶意输入
If LXH > 0 And LXH <= 16384 Then
ad1 = Range(Cells(1, LXH), Cells(1, LXH)).AddressLocal
ad1 = Mid(ad1, 2, Len(ad1) - 1)
GetColNameXLS = Mid(ad1, 1, InStr(1, ad1, "$", vbTextCompare) - 1)
Else
GetColNameXLS = "不合理的值(≤0或>16384)"
End If
End Function
[ 本帖最后由 厨师王德榜 于 2014-10-11 10:53 编辑 ]

