excel替换不同字符位置的多个实例
我有一个来自sql数据库的数据集,大约有6500行,其中一列包含包含多个值的字段excel替换不同字符位置的多个实例,excel,excel-formula,vba,Excel,Excel Formula,Vba,我有一个来自sql数据库的数据集,大约有6500行,其中一列包含包含多个值的字段 Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF 3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT 10.00ABC 1.00DDD 2.00EEE 1.00DEF 范例
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
范例
Cell A1 3.00GGG1.00DDD2.00EEE1.00DEF5.00TTT
Cell A2 10.00ABC1.00DDD2.00EEE1.00DEF
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
.00是字符串中唯一一致的字符序列我需要引入一个“,”以便能够使用文本到列工具将值拆分为不同的列
一个复杂的问题是小数点左边的数字可能不同,可能是10或更大,因此逗号的位置可能是小数点左边的1或2个位置
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
所以我需要实现一个目标
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
最终通过使用文本到列来实现
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
有人知道如何通过公式或宏实现这一点吗
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
谢谢 我用正则表达式为你写了一些东西。它确实有一个漏洞,试试这个,如果它不起作用,我们将弥补这个漏洞
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
call findMatch("3.00GGG1.00DDD2.00EEE1.00DEF5.00TTT ","[A-Za-z]\d")
Function findMatch(strValue, strPattern)
Dim objRegEx
Dim objPosition
Dim strPosition
strNewVal = strValue
'find out how many different strings will be there
countOfStrings = len(strNewVal) - len(replace(strNewVal, ".", ""))
strCompiledString = ""
'Create regular expression
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strPattern
'loop to break the string up and tie it with comma
for i= 1 to countOfStrings
if not objRegEx.Test(strValue) then
'if condition to check whether or not there are any matches
strCompiledString = strCompiledString & "," & strValue
exit for
end if
Set objPosition = objRegEx.Execute(strValue)
strPosition = objPosition(0).FirstIndex
findMatch= strPosition
'based on match index, add the left part of the string to the variable strCompiledString
if(strCompiledString="") then
strCompiledString = mid(strValue,1,strPosition+1)
else
strCompiledString = strCompiledString & "," & mid(strValue,1,strPosition+1)
end if
'remove the already processed substring from the main string
strValue = mid(strValue,strPosition+2)
next
'now we have it
msgbox("BOOOOM : " & strCompiledString)
End Function
我从来没想过我会这么说,但对我来说,它实际上是用词来表达的
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
将行复制到Word文档。打开查找/替换(CTRL+H)
,单击更多>>
打开高级菜单并勾选框使用通配符
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
然后输入Find What
([0-9]{1,2}.)
,并输入Replace with,\1
。单击“全部替换”并将所有内容复制回excel工作表。我修改了一个自定义项,用于从字符串中获取第n个数字,以获取第n个数字加字母:-
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
Function GetNthNumber(s As String, n As Integer) As String
Dim c, testNumber, number As String
s = s & "0"
Dim i, j, count As Integer
Dim inNumber As Boolean
inNumber = False
' Loop through each character of string
For i = 1 To Len(s)
c = Mid(s, i, 1)
' Part of a number - append new character or finish number
If inNumber Then
If Not (IsNumeric(number & c)) Then
inNumber = False
End If
number = number & c
Else
' Not part of a number - start new number or do nothing
If IsNumeric(c) Then
If count = n Or i = Len(s) Then Exit For
inNumber = True
number = c
count = count + 1
Else
number = number + c
End If
End If
Next i
'Return nth number or #Value error
If count >= n Then
GetNthNumber = number
Else
GetNthNumber = CVErr(xlErrValue)
End If
End Function
要使用它,请调用该函数以获取第一个数字,并将其拉过,直到它出错为止
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF
=IFERROR(GetNthNumber($A1,COLUMN(A:A)),"")
是GGG等字母还是其他非数字字符?您好,它们是字母谢谢
Cell A1 ,3.00GGG,1.00DDD,2.00EEE,1.00DEF,5.00TTT
Cell A2 ,10.00ABC,1.00DDD,2.00EEE,1.00DEF
3.00GGG 1.00DDD 2.00EEE 1.00DEF 5.00TTT
10.00ABC 1.00DDD 2.00EEE 1.00DEF