假设我拥有一个大型数据库,其中包含不同种类的数据,例如567、6788、456,并混合有其他类型的数据,例如:45.67; 78.78;等等。我正在尝试转换所有没有中间值的值,例如56.7中的567、67.88中的6788等。我尝试了一种方法:将数据分为两列,然后在Excel中使用concat公式,但如果遇到前导零,则不会保持前导零。由于我的数据库很大,有人知道公式会自动对其执行操作吗?谢谢
答案 0 :(得分:-1)
在VBA中,您可以使用正则表达式来执行此操作->使用正则表达式:“(\ d +)”
这是可以用于此的功能。
Public Function RegexExtractAll(ByVal text As String, _
ByVal extract_what As String, _
Optional separator As String = "") As String()
' This function takes a text, a regex expression and an optional separator character (default is "").
' It uses vbscript.regexp to delivers all matches and sub matches of the regex as a string array.
Dim allMatches As Variant
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
Dim i As Long, j As Long
Dim result() As String
RE.Pattern = extract_what
RE.Global = True
On Error Resume Next
Set allMatches = RE.Execute(text)
If Not (IsEmpty(allMatches) Or (allMatches.Count = 0)) Then
ReDim result(allMatches.Count - 1)
For i = 0 To allMatches.Count - 1
For j = 0 To allMatches.Item(i).submatches.Count - 1
result(i) = result(i) & (separator & allMatches.Item(i).submatches.Item(j))
Next
Next
End If
RegexExtractAll = result
End Function 'RegexExtractAll
要测试它是否有效:
Sub SomeTest()
' The RegexExtractAll will return an array with the matches.
' By using Join() we merge them to one string.
Debug.Print Join(RegexExtractAll("655.44,32", "(\d+)"), "")
End Sub
答案 1 :(得分:-1)
'45.67 ---> 45.67
'78.78 ---> 78.78
'567 ---> 56.7
'6788 ---> 67.88
Option Explicit
Public Function add_dot(actRange As Range) As String
If actRange.Value Like "*.*" Then
add_dot = actRange.Value
Else
add_dot = Left(actRange.Value, 2) & "." & Mid(actRange.Value, 2) ', Len(actRange.Value) - 2)
End If
End Function