zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ExcelVBA把输入数字转化为大写字母

输入 数字 转化 ExcelVBA 大写字母
2023-06-13 09:13:56 时间

问题:我们在Excel中的界面中列是用字母ABCD....显示的,当然在程序运行中也可以用数字,所以变出现了数字与字母互换的问题

=====以下是学习收集的代码====

'代码一:
Public Sub NumberToUpperCase1()
    Dim x As Variant, y As String
    x = InputBox("请输入数字")
    If IsNumeric(x) And x < Columns.Count Then
        y = Replace(Cells(1, Val(x)).Address(0, 0), "1", "")
        MsgBox "数字" & x & "转化为列标为:" & y
    Else
        MsgBox "输入的数据类型有误或超出范围。"
    End If
End Sub

.


'代码二:
Public Sub NumberToUpperCase2()
    Dim x As Variant, y As String
    x = InputBox("请输入数字")
    If IsNumeric(x) And x < Columns.Count Then
        y = Left(Cells(1, Val(x)).Address(0, 0), Len(Cells(1, Val(x)).Address(0, 0)) - 1)
        MsgBox "数字" & x & "转化为列标为:" & y
    Else
        MsgBox "输入的数据类型有误或超出范围。"
    End If
End Sub

.


'代码三:
Public Sub NumberToUpperCase3()
    Dim x As Variant, y As String
    x = InputBox("请输入数字")
    If IsNumeric(x) And x < Columns.Count Then
        y = Mid(Cells(1, Val(x)).Address(0, 0), 1, Len(Cells(1, Val(x)).Address(0, 0)) - 1)
        MsgBox "数字" & x & "转化为列标为:" & y
    Else
        MsgBox "输入的数据类型有误或超出范围。"
    End If
End Sub

.


'代码四:
Public Sub NumberToUpperCase4()
    Dim x As Variant, y As String
    x = InputBox("请输入数字")
    If IsNumeric(x) And x < Columns.Count Then
        y = Split(Cells(1, Val(x)).Address, "$")(1)
        MsgBox "数字" & x & "转化为列标为:" & y
    Else
        MsgBox "输入的数据类型有误或超出范围。"
    End If
End Sub

.


'代码五:此代码实质是进制转换,即把10进制转换为26进制。只用VB函数,只受Long型数据限制。
Public Sub NumberToUpperCase5()
    Dim x As Variant, y As String
    x = InputBox("请输入数字")
    y = NbToUc(x)
    MsgBox y
End Sub

Public Function NbToUc(lonVal As Variant)
    Dim lonX As Long
    If IsNumeric(lonVal) And lonVal <= 2147483647 Then
        NbToUc = ""
        Do
            lonX = lonVal Mod 26
            NbToUc = Chr(lonX + 64) & NbToUc
            lonVal = lonVal \ 26
        Loop Until lonVal = 0
    Else
        NbToUc = "输入的数据类型有误或超出范围。"
    End If
End Function

====今天学习到此====