例如,我们的单元格包含:
EWFS 410461, 501498, EFW406160
所以,我需要返回
的公式410461 501498 406160
答案 0 :(得分:4)
请考虑以下用户定义功能:
Public Function GetNumbers(s As String) As String
Dim L As Long, i As Long, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
L = Len(s)
For i = 1 To L
If Mid(s, i, 1) Like "[A-Z]" Or Mid(s, i, 1) = "," Then Mid(s, i, 1) = " "
Next i
GetNumbers = wf.Trim(s)
End Function
所有数字都将作为空格分隔的字符串
返回答案 1 :(得分:3)
如果您拥有Office 365,则可以使用以下数组公式:
=TEXTJOIN(" ",TRUE,IF((ISNUMBER(--MID(A1,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)-5)),6)))*(NOT(ISNUMBER(--MID(A1&";",ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)-5)),7)))),MID(A1,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)-5)),6),""))
作为数组公式,退出编辑模式时必须使用Ctrl-Shift-Enter而不是Enter进行确认。
答案 2 :(得分:1)
如果只有“ E”,“ W”,“ F”和“ S”是必须除去的字母,则可以避免使用VBA并使用SUBSTITUTE()函数:
=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B2,"E",""),"W",""),"F",""),"S",""),",",""))
答案 3 :(得分:1)
加里学生的答案略有不同:
Public Function GetNumbers2(s As String) As String
Dim i As Long, elem As Variant
For Each elem In Split(s, ",")
For i = 1 To Len(elem)
If Mid(elem, i, 1) Like "[0-9]" Then Exit For
Next i
GetNumbers2 = GetNumbers2 & " " & Application.WorksheetFunction.Trim(Mid(elem, i))
Next
GetNumbers2 = Trim(GetNumbers)
End Function
答案 4 :(得分:0)
这个答案比得分为正的答案要好,但我更喜欢使用ASCII codes处理字符串中的字符。这样可以使范围以Select Statements
清晰地组织起来。这对于拒绝像我父母这样的老练用户(我没有给他们的孙子命名为“ 4”)中的字符特别有用。
以下是适用于OP的UDF,但同时也展示了如何利用the VBA Asc function与select statement组合来处理,大写/小写或任何其他特定字符:>
Public Function GiveTheNumbers(theINPUT As String) As String
Dim p As Long, aCode As Long
For p = 1 To Len(theINPUT)
aCode = Asc(Mid(theINPUT, p, 1)) 'converts string to an ascii integer
Select Case aCode
'32 is the ascii code for space bar. 48 to 57 is zero to nine.
Case 32, 48 To 57
GiveTheNumbers = GiveTheNumbers & Chr(aCode) 'Chr() converts integer back to string
'the rest of these cases are not needed for the OP but I'm including for illustration
Case 65 To 90
'all upper case letters
Case 97 To 122
'all lower case letters
Case 33, 64, 35, 36, 37, 42
'my favorite characters of: !@#$%*
Case Else
'anything else
End Select
Next p
End Function
答案 5 :(得分:-1)
=NDIGITS($A1,6)
'******************************************************************************
' Purpose: From a string, returns digit groups (numbers) in a delimited
' string.
' Inputs
' SourceString - Required. The string to be checked for digits.
' NumberofDigits - Optional. The number of digits in digit groups. If 0,
' all digit groups are returned. Default: 0.
' TargetDelimiter - Optional. The delimiter of the returned string.
' Default: " " (space).
'******************************************************************************
Function NDigits(ByVal SourceString As String, _
Optional ByVal NumberOfDigits As Long = 0, _
Optional ByVal TargetDelimiter As String = " ") As String
Dim i As Long ' SourceString Character Counter
Dim strDel As String ' Current Target String
' Check if SourceString is empty (""). Exit if. NDigits = "".
If SourceString = "" Then Exit Function
' Loop through characters of SourceString.
For i = 1 To Len(SourceString)
' Check if current character is not a digit (#), then replace with " ".
If Not Mid(SourceString, i, 1) Like "#" Then _
Mid(SourceString, i, 1) = " "
Next
' Note: While VBA's Trim function removes spaces before and after a string,
' Excel's Trim function additionally removes redundant spaces, i.e.
' doesn't 'allow' more than one space, between words.
' Remove all spaces from SourceString except single spaces between words.
strDel = Application.WorksheetFunction.Trim(SourceString)
' Check if current TargetString is empty (""). Exit if. NDigits = "".
If strDel = "" Then Exit Function
' Replace (Substitute) " " with TargetDelimiter if it is different than
' " " and is not a number (#).
If TargetDelimiter <> " " And Not TargetDelimiter Like "#" Then
strDel = WorksheetFunction.Substitute(strDel, " ", TargetDelimiter)
End If
' Check if NumberOfDigits is greater than 0.
If NumberOfDigits > 0 Then
Dim vnt As Variant ' Number of Digits Array (NOD Array)
Dim k As Long ' NOD Array Element Counter
' Write (Split) Digit Groups from Current Target String to NOD Array.
vnt = Split(strDel, TargetDelimiter)
' Reset NOD Array Element Counter to -1, because NOD Array is 0-based.
k = -1
' Loop through elements (digit groups) of NOD Array.
For i = 0 To UBound(vnt)
' Check if current element has number of characters (digits)
' equal to NumberOfDigits.
If Len(vnt(i)) = NumberOfDigits Then
' Count NOD Array Element i.e. prepare for write.
k = k + 1
' Write i-th element of NOD Array to k-th element.
' Note: Data (Digit Groups) are possibly being overwritten.
vnt(k) = vnt(i)
End If
Next
' Check if no Digit Group of size of NumberOfDigits was found.
' Exit if. NDigits = "".
If k = -1 Then Exit Function
' Resize NOD Array to NOD Array Element Count, possibly smaller,
' due to fewer found Digit Groups with the size of NumberOfDigits.
ReDim Preserve vnt(k)
' Join elements of NOD Array to Current Target String.
strDel = Join(vnt, TargetDelimiter)
End If
' Write Current Target String to NDigits.
NDigits = strDel
End Function
'******************************************************************************
' Remarks: A digit group are consecutive numbers in the string e.g.
' in the string "123 sdf jk 23 4" there are three digit groups:
' The 1st is 123 with NumberOfDigits = 3, the 2nd is 23 with
' NumberOfDigits = 2 and finally 4 with NumberOfDigits = 1. Since
' they all have a different number of digits, all will be returned
' if NumberOfDigits is 0 or omitted, otherwise only one will be
' returned.
'******************************************************************************
答案 6 :(得分:-4)
使用Right()
函数并获取6个最右边的字符。例如:
Right(cell.Value, 6)
其中cell
是一些Range
变量,用于寻址相关单元格
例如
Dim cell As Range
For Each cell In Range("B2:D2") ' change "B2:D2" to your actual range woth values
Debug.Print Right(cell.Value, 6)
Next