如何从定界词中分离出1个词

时间:2018-08-21 13:19:56

标签: vba excel-vba

我遇到的一种情况是我要从SAP提取数据,然后将其分解并提交报告。不幸的是,我要提取的数据是由不同国家/地区的多个人制作的,因此,每个格式都略有不同。当电话号码和名称更改时,似乎它们总是列出“电话”,后跟数字。我想在这个数字之后进行拆分,然后将其取整。是否有一种方法可以从所选的分隔符(在本例中为“电话”)中将语句拆分成一个单词(或字符串)?

08.08.2018 19:51:54 UTC "Person's Name" (CODE) Phone +##########
Reporter is the pharmacist. She states a patient reported her Device will not work, and if she is able to get it to work no medication comes out. Patient is an experienced user of The medicine. Pharmacy to replace to patient. Company to replace to pharmacy. Pharmacy to return product.
09.08.2018 11:47:16 "Person's Name" (Code) Phone +##########
On 09-AUG-2018, Company received 1 Device from lot #######, 07-2019, 09.08.2018 20:36:32 UTC "Person's name" (Code) Phone +##########

1 个答案:

答案 0 :(得分:0)

如果我没记错的话,您希望电话号码之后的所有内容,在您的示例中,他们都不希望电话号码之后的任何内容,但是此代码将打印从Sheet1到Sheet2的电话号码之后的所有内容:

Sub GetMyText()
    cnt = 0 'number of cells containing string "Phone"
    For Each cell In Sheets("Sheet1").UsedRange 'change this line based on the Sheet name you have
        If InStr(cell.Value, "Phone") > 0 Then
            cnt = cnt + 1
            a = Split(cell.Value, "Phone")
            b = Trim(a(1)) 'get the second part and remove the leading and trailing space
            pos = InStr(b, " ") 'find the first space
            If pos > 0 Then
                Sheets("Sheet2").Cells(cnt, 1).Value = Right(b, Len(b) - pos) 'change the name of the sheet if needed or have sheet names Sheet2
            End If
        End If
    Next cell
End Sub