简体   繁体   English

Excel宏无法分隔字符串地址

[英]Excel Macro Unable to Separate String Address

Software: MS Excel 2016 软体: MS Excel 2016


Update 1 更新1

Please note there can be any number of digits before West, ie 请注意,西之前可以有任意数字,即

123124234234West18th Street

2West 14th Avenue

12324West

Please assist with general solution 请协助一般解决方案



Original Question 原始问题

There is address, 31West 52nd Street I am trying to split the 31 and West so output will be 地址为31 West 31West 52nd Street我想将31West分开,所以输出将是

31 West 52nd Street

Tried this Macro statement but it won't work, please guide 尝试了此宏语句,但无法正常工作,请指导

Selection.Replace What:="?@West ", Replacement:=" West " _ , LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat _ :=False, ReplaceFormat:=False

This is a sample of code, that would check for the first few chars. 这是代码示例,将检查前几个字符。 If they are digits, if would split them with a space from the rest: 如果它们是数字,则将它们与其他空格分开:

Option Explicit

Public Sub TestMe()

    Debug.Print fnStrStripMyNumber("31West  52nd Street")
    Debug.Print fnStrStripMyNumber("123Vityata Shampion")

End Sub

Public Function fnStrStripMyNumber(strStr As String) As String

    Dim lngCountDigits  As Long
    Dim lngCounter      As Long

    strStr = Trim(strStr)

    For lngCounter = 1 To Len(strStr)
        If IsNumeric(Mid(strStr, lngCounter, 1)) Then
            lngCountDigits = lngCountDigits + 1
        Else
            Exit For
        End If
    Next lngCounter

    strStr = Left(strStr, lngCountDigits) & " " & Right(strStr, Len(strStr) - lngCountDigits)
    fnStrStripMyNumber = Trim(strStr)

End Function

Thus, from input: 因此,从输入:

"31West  52nd Street"
"123Vityata Shampion"

We get output: 我们得到输出:

31 West  52nd Street
123 Vityata Shampion

You can try this excel formula as well, 您也可以尝试使用此excel公式,

=LEFT(A1,FIND("West",A1)-1)&" "&RIGHT(A1,LEN(A1)-FIND("West",A1)+1)

在此处输入图片说明

Or if you want a macro only, 或者,如果您只想要宏,

Sub rep()
Range("B1") = Replace(Range("A1"), "West", " West")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM