excel替换不同字符位置的多个实例

时间:2016-12-01 07:08:24

标签: excel excel-vba excel-formula vba

我有一个来自sql数据库的数据集,其中有大约6500行,其中一列包含多个值的字段。

实施例

Cell A1 3.00GGG1.00DDD2.00EEE1.00DEF5.00TTT             
Cell A2 10.00ABC1.00DDD2.00EEE1.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     

有没有人有任何想法如何通过公式或宏来实现这一目标?

谢谢!

3 个答案:

答案 0 :(得分:1)

我从未想过我会这么说,但对我而言,它确实可以使用Word。

将您的行复制到Word文档。打开查找/替换(CTRL + H),点击More>>打开高级菜单,然后勾选方框Use wildcards

然后输入查找内容([0-9]{1,2}.)和“替换为,\1”。单击全部替换并将所有内容复制回Excel工作表。

答案 1 :(得分:0)

我使用RegEx为您写了一些东西。它确实有一个漏洞,尝试这个,如果它不起作用,我们将在漏洞上工作。

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

答案 2 :(得分:0)

我修改了一个我写的UDF,用于从字符串中获取第n个数字以获取第n个数字加上字母: -

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

要使用它,请调用该函数以获取第一个数字,然后将其拉出直至错误输出

=IFERROR(GetNthNumber($A1,COLUMN(A:A)),"")

enter image description here