【问题标题】:Convert column letter to number将列字母转换为数字
【发布时间】:2018-08-10 08:54:08
【问题描述】:

我从查询中获取原始数据到 Excel,在执行 VLOOKUP 时,有时我必须手动计算或计算我要引用的列。

如果我在用户表单文本框中输入例如“M”,我想要一个计算器,而另一个文本框将显示“M”的正确列号 (13)。

我的用户表单如下所示:

我想出了类似下面代码的东西,我将每个字母调暗为一个整数,当它输入到文本框中时,它会相互添加值。

我不知道如何编写 CommandButton1_click "Räkna"。

Private Sub CommandButton1_Click()

    'how do i transform letters into numbers here?

End Sub

Sub raknare()

Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Dim g As Integer
Dim h As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As Integer
Dim o As Integer
Dim p As Integer
Dim q As Integer
Dim r As Integer
Dim s As Integer
Dim t As Integer
Dim u As Integer
Dim v As Integer
Dim w As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer

Set a = 1
Set b = 2
Set c = 3
Set d = 4
Set e = 5
Set f = 6
Set g = 7
Set h = 8
Set i = 9
Set j = 10
Set k = 11
Set l = 12
Set m = 13
Set n = 14
Set o = 15
Set p = 16
Set q = 17
Set r = 18
Set s = 19
Set t = 20
Set u = 21
Set v = 22
Set w = 23
Set x = 24
Set y = 25
Set z = 26

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    要从对话中获取信息到变量,您需要执行类似的操作

    Dim v As Variant
    v = Application.InputBox(Prompt:="Letter: ", Title:="Letter", Type:=2)
    If CBool(v) Then ' The inputbox returns "false" if cancel is pressed
      ...
    EndIf
    

    如果你想使用用户表单,你会做类似的事情

    Dim s As String
    s = UserForm1.TextBox1.Text
    

    要从名称中获取列号,您可以执行this answer 中描述的类似操作。

    或者做我在办公室做过的事情,自己算术:

    【讨论】:

    • 哈哈,我喜欢这张照片,我现在有点使用这种技术。当我在数据列中达到 BR 并且必须手动计算时,我的问题就开始了,有时我弄错了。我只想通过添加一个为我完成这一切的用户表单来简化!但是给我几分钟,我会试试你的代码,谢谢你的回答!
    • @sam 看着我链接的那个答案可能对你也有一些帮助。
    • 它不是我要找的代码,因为我想在用户表单文本框中编写代码。
    • @sam 除了使用debug.print,您可以使用例如UserForm1.TextBox2 = Range(ColName & 1).Column
    • @sam 我能想到的主要困难是验证 textbox1 的内容将始终是有效的列名...当然,如果只有您将使用该工具,例如可能不需要验证。
    【解决方案2】:

    我解决了我的问题!但不是我最初打算的方式。我从以前的项目中获取了一些旧代码,并将其用于解决这个问题。

    我制作了一张名为“DATA”的表格,并在 A 列中插入了 A 到 CW 的字母表,在其旁边我有每个字母 1-100 对应的数字。

    然后我做了一个看起来像这样的搜索功能:

    Sub rakna()
    
        Dim rSearch As Range
        Dim rFound As Range
    
        With Sheets("DATA")
            Set rSearch = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
    
            Set rFound = rSearch.Find(What:=TextBox1.Text, LookIn:=xlValues)
    
            If rFound Is Nothing Then
                TextBox2.Value = ""
            Else
                TextBox2.Value = rFound.Offset(0, 1).Value
    
    
            End If
        End With
    
    
    End Sub
    

    现在我不再需要计算列,我可以在我的文本框中输入我需要的列!

    【讨论】:

      【解决方案3】:

      你可以试试:

      Option Explicit
      
      Sub test()
      
          Dim Letter As String
          Dim LetterNumber As Long
      
          Letter = "F"
          LetterNumber = Range(Letter & 1).Column
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2019-03-25
        • 1970-01-01
        • 2016-07-07
        • 1970-01-01
        • 2021-08-03
        相关资源
        最近更新 更多