0

我有一个包含数百行的 .xlsm 文档,我想根据 B 列设置 C 列的值。

例如:B1 包含“+ 新节点‘145872-12547-4885’已创建”

我的目的是使用一个宏来提取,该宏循环所有行,只是两个“'”之间的文本..在这种情况下——145872-12547-4885——

提前致谢,

Sub extract()

    Dim N As Long, i As Long, j As Long


    N = Cells(Rows.Count, "B").End(xlUp).Row

    j = 1

    For i = 1 To N

           If Left(Cells(i, "B"), 11) = " + New node" Then

           Cells(j, "C").Value = Mid(Cells(i, "B"), InStr(1, Cells(i, "B").Value, "'") + 1, Len(Cells(i, "B")) - InStr(1, Cells(i, "B").Value, "'") - 1)

            j = j + 1

          End If

    Next i

End Sub

谢谢,但我还是有一些麻烦,

我希望提取物位于包含原始值的单元格前面,但事实并非如此:

在此处输入图像描述, 因为我也需要提取以“SEL_AFFILIATE”结尾的单元格开头的代码,我不知道如何使用 SPLIT 这是我的代码:

Sub extract()

Dim N As Long, i As Long, j As Long, s As String

N = Cells(Rows.Count, "B").End(xlUp).Row

j = 1

For i = 1 To N

        s = Cells(i, "B").Text

            If Left(s, 11) = " + New node" Then

                Cells(j, "C").Value = Split("'" & s, "'")(2)

                j = j + 1

            End If

            'If Right(s, 14) = "SEL_AFFILIATE " Then

               ' Cells(j, "C").Value = Split("" & s, ".")(2)

               ' j = j + 1

            'End If          
Next i
End Sub
4

3 回答 3

1

你可以使用Split()

Sub extract()

    Dim N As Long, i As Long, j As Long, s As String


    N = Cells(Rows.Count, "B").End(xlUp).Row

    j = 1

    For i = 1 To N
            s = Cells(i, "B").Text
           If Left(s, 11) = " + New node" Then
                Cells(j, "C").Value = Split("'" & s, "'")(2)
            j = j + 1
          End If
    Next i

End Sub

在此处输入图像描述

编辑#1:

有关该Split()功能的说明,请参见:

参考

于 2019-08-10T11:25:31.983 回答
0

尝试:

Sub extract()

Dim N As Long, i As Long, j As Long, s As String

N = Cells(Rows.Count, "B").End(xlUp).row


For i = 1 To N

        s = Cells(i, "B").Text

            If Left(s, 11) = " + New node" Then

                Cells(i, "B").Offset(0, 1).Value = Split("'" & s, "'")(2)


            End If

            If Right(s, 14) = "SEL_AFFILIATE " Then

                Cells(i, "B").Offset(0, 1).Value = Split(s, ".")(0)

            End If

Next i

End Sub
于 2019-08-10T12:57:24.977 回答
0

现在我想提取我的单元格内容的结尾,如果 begin = "In File"

这是我的牢房

我尝试了这段代码但徒劳无功:

        If Left(s, 8) = " In File" Then

            Cells(i, "B").Offset(0, 1).Value = Split(",", s)(0)

        End If
于 2019-08-10T13:38:11.967 回答