gold_star
- 方法一:
'列數轉字母
Function CNtoW(ByVal num As Long) As String
CNtoW = Replace(Cells(1, num).Address(False, False), "1", "")
End Function
'字母轉列數
Function CWtoN(ByVal AB As String) As Long
CWtoN = Range("a1:" & AB & "1").Cells.Count
End Function
- 方法二:
Function GetColumn(char)
Dim t
On Error Resume Next
If IsNumeric(char) Then
t = Split(Cells(1, char).Address, "$")(1)
Else
t = Columns(char & ":" & char).Column
End If
If Err.Number <> 0 Then GetColumn = "超出范圍" Else GetColumn = t
End Function
3,方法三:
不過就功能、目的來說,確實意義不大。
因為既然用到了VBA,就可以后台使用多種方法處理列數,沒必要再寫一個函數來用。
比如, cells(n,"AB") 或者 Range("AB" & n) 就可以直接定位到指定列標所在列。
或者用 cells(n,28).Address 來獲取列標字母。
如同樓上各位的例子那樣。
而如果需要自己計算把列序號轉換為字母標簽,則可以用下面迭代計算:
假設t為列序號,那么:
s = ""
Do
s = Chr((t - 1) Mod 26 + 65) & s '以26除數反複Mod求余得到對應1-26字母
t = Int((t - 1) / 26) '再用26除后Int取整進行迭代計算
Loop Until t <= 0 '反複算直至數t已不能除26求余止
+++++++
Function f(t)
Do
f = Chr((t - 1) Mod 26 + 65) & f
t = Int((t - 1) / 26)
Loop Until t = 0
End Function
+++++++
或者寫成:
+++++++
Function f(t)
Do
f = Chr((t - 1) Mod 26 + 65) & f
t = (t - 1) \ 26
Loop While t
End Function
+++++++
(本文主要內容歸納自網上文章:EXCEL列標數字與字母的轉換)