-1

我是 excel VB 的新手,并在以下问题中寻求帮助:

我有一列 A 具有以下值:

column A
"VL50s"
"M50s"
"H50s"
"VL50s"
"H50s"

我想提取数字并将下面的算术函数运行到 B 列中。

key:
x is a number
VLx --> (x) + 1
Mx -->(x) + 2
Hx --> (x) + 3

使用上面的键,输出应如下所示:

coloumn B
51
52
53
51
53

我想问一下我将如何在 VBA 中执行此功能。谢谢您的帮助。

4

3 回答 3

1

因为您说字母/数字组合的数量比您的示例中的要多得多,所以我认为这是 VBA 的问题,而不是工作表函数。一个 WS 功能将变得难以维护并且非常迅速。

我做了这4个功能。该GetCharArray函数解析您传递给它的字符串的文本,以将该文本作为字符数组返回(即使 BA 没有char类型只是一个string类型,所以我返回一个字符串。同样的想法)

然后假设我们可以调用GetNumberFromChars以获取50fromVL50s并调用GetLeftMostLetters以获取VLfrom VL50s

然后是一些工作表,我创建了一个命名范围keys,其中范围的第 1 列是字母,如“VL”、“H”、“M”......并且与之关联的相应值在第 2 列中。它看起来像

Col1    Col2
VL      1
M       2
H       3
...     ...

我们可以使用带有和结果的vlookup工作表函数来找到应该添加到结果中的数字。Range("keys")GetLeftMostLettersGetNumberFromChars

Function GetNewNumber(inString As String) As Double
    Dim searchString As String, numberToAddFromKeys As Double, numberToAddToFromCell As Long, cellChars() As String

    cellChars = GetCharArray(inString)
    searchString = GetLeftMostLetters(cellChars)
    numberToAddToFromCell = GetNumberFromChars(cellChars)

    'use the keys named range where column 1 is your letters ("VL","H"...)
    'and column 2 is the corresponding value for that letter set
    numberToAddFromKeys = WorksheetFunction.VLookup(searchString, Range("keys"), 2, 0)

    GetNewNumber = CDbl(numberToAddFromKeys) + CDbl(numberToAddToFromCell)
End Function


Function GetNumberFromChars(inChars() As String) As Long
    Dim returnNumber As String, i As Long, numberStarted As Boolean

    For i = 1 To UBound(inChars)
        If IsNumeric(inChars(i)) Then
            If Not numberStarted Then numberStarted = True
            returnNumber = returnNumber & inChars(i)
        Else
            If numberStarted Then
                'this will ignore that "s" on the end of your sample data
                'hopefully that's what you need
                GetNumberFromChars = returnNumber
                Exit Function
            End If
        End If
    Next
End Function

Function GetLeftMostLetters(inChars() As String) As String
    Dim returnString As String, i As Long

    For i = 1 To UBound(inChars)
        If Not IsNumeric(inChars(i)) Then
            returnString = returnString & inChars(i)
        Else
            GetLeftMostLetters = returnString
        End If
    Next
End Function


Function GetCharArray(inText As String) As String()
    Dim s() As String, i As Long
    ReDim s(1 To Len(inText))

    For i = 1 To UBound(s)
      s(i) = Mid$(inText, i, 1)
    Next
    GetCharArray = s
End Function

所以可以这样使用...

Dim cell As Range, rng As Range
'set this range to your actual range. 

Set rng = Sheets("your sheet name").Range("A1:A5")       
For Each cell In rng
    'put this resulting value wherever you want.
    Debug.Print GetNewNumber(cell.Value)
Next cell
于 2012-08-06T15:26:07.637 回答
0

这将返回在输入值中找到的第一个数字:

Function GetNumber(val)
    Dim re As Object
    Dim allMatches
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "(\d+)"
    re.ignorecase = True
    re.Global = True
    Set allMatches = re.Execute(val)
    If allMatches.Count > 0 Then
        GetNumber = allMatches(0)
    Else
        GetNumber = ""
    End If
End Function

编辑:刚刚注意到你的问题标题是“小数”数字 - 你的值会有小数位,还是全部都是整数?

于 2012-08-06T16:54:28.707 回答
0

您甚至不必为此使用 VBA,您可以使用(非常丑陋的)公式来确定这一点:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, "VL",""), "M",""), "H", ""), 
    "s", "") + IF(LEFT(A1, 2) = "VL", 1, IF(LEFT(A1, 1) = "M", 2, 
    IF(LEFT(A1,1) = "H", 3, 0)))

实际上,这个公式应该在一行上,但我在这里把它分解了,这样它就可以阅读了。将公式放在单元格 B1 中,然后将其复制到您需要的任何其他单元格中。它去掉“VL”、“M”、“H”和“s”的所有实例,然后根据 A 单元格的左侧 1 或 2 个字符添加额外的数字。

于 2012-08-06T12:52:27.800 回答