将列字母转换为数字

时间:2018-08-10 08:54:08

标签: excel vba

我从查询中获取原始数据到Excel中,并且在执行VLOOKUP时,有时我不得不手工计算或计算要引用的列。

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

我的用户表单如下:

enter image description here

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

我不知道如何编写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

3 个答案:

答案 0 :(得分:0)

要从对话中获取有关变量的信息,您可以执行类似的操作

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中所述的操作。

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

enter image description here

答案 1 :(得分:0)

我解决了我的问题!但不是我最初打算的方式。我从以前的项目中获得了一些旧代码,并使其在此问题上起作用。

我制作了一个名为“ 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

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

答案 2 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    Dim Letter As String
    Dim LetterNumber As Long

    Letter = "F"
    LetterNumber = Range(Letter & 1).Column

End Sub