傳入數值,傳出Excel的ColumnLabel的函數
MsgBox(GetExcelColumnLabel(1))
MsgBox(GetExcelColumnLabel(26))
MsgBox(GetExcelColumnLabel(27))
MsgBox(GetExcelColumnLabel(52))
MsgBox(GetExcelColumnLabel(53))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 傳入數值,傳出Excel的ColumnLabel
''' </summary>
''' <param name="vColumnCount"></param>
''' <returns></returns>
''' <remarks>
''' 1=>A
''' 26=>Z
''' 27=>AA
''' </remarks>
''' -----------------------------------------------------------------------------
Private Function GetExcelColumnLabel(ByVal vColumnCount As Integer) As String
'2010/01/06 RM 改用RANGE去ASSIGN DATA
Const cnExcelColumnMax As Integer = 26
'A~Z, AA~AZ, BA~BZ
Dim strPreColumn As String = String.Empty
Dim intColumnOffset As Integer = vColumnCount \ cnExcelColumnMax
Dim intColumnMod As Integer = vColumnCount Mod cnExcelColumnMax
If intColumnMod = 0 Then
intColumnMod = cnExcelColumnMax
intColumnOffset -= 1
End If
Dim strPostColumn As String = String.Empty
strPostColumn = Chr(64 + intColumnMod)
If intColumnOffset > 0 Then
strPreColumn = Chr(64 + intColumnOffset)
End If
Return strPreColumn & strPostColumn
End Function
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^