我是excel VB的新手,并在以下问题中寻求助手:
我有一个列A,其中包含以下值:
column A
"VL50s"
"M50s"
"H50s"
"VL50s"
"H50s"
我想提取数字并在下面的coloumn B中运行以下算术函数。
key:
x is a number
VLx --> (x) + 1
Mx -->(x) + 2
Hx --> (x) + 3
使用上面的键输出应如下所示:
coloumn B
51
52
53
51
53
我想问一下如何在VBA中执行此功能。谢谢您的帮助。
答案 0 :(得分:1)
因为你说字母/数字组合的数量远远大于你的例子,我认为这是VBA的问题而不是工作表函数。 WS功能将变得难以维护并且非常快速。
我做了这4个功能。 GetCharArray
函数解析传递它的字符串的文本,将该文本作为字符数组返回(即使BA没有char
类型只有string
类型,所以我我正在返回一个字符串。同样的想法)
然后我们可以致电GetNumberFromChars
来50
来VL50s
,并致电GetLeftMostLetters
从VL
获取VL50s
。
然后是一些工作表,我创建了一个名为keys
的命名范围,其中范围的第1列是字母,如“VL”,“H”,“M”......并且与之关联的相应值是第2列。它看起来像
Col1 Col2
VL 1
M 2
H 3
... ...
我们可以将vlookup
工作表函数与Range("keys")
和GetLeftMostLetters
的结果一起使用,以查找应添加到GetNumberFromChars
结果中的数字。
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
答案 1 :(得分: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个字符添加额外的数字。
答案 2 :(得分: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
编辑:刚刚注意到你的问题标题是“十进制”数字 - 你的值是否会有小数位,或者全部都是整数?