vba 从单元格值中提取两个特殊字符之间的单词的程序
vba program to extract word between two special caracters from cells value
我有一个包含数百行的 .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
谢谢,但我还有一些问题,
我希望提取内容位于包含原始值的单元格前面,但事实并非如此:
enter image description here
因为我也需要提取以 "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
你可以使用 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()
函数的说明,请参阅:
尝试:
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
现在我想提取单元格内容的结尾如果开头 = "In File"
This is my cell
我试过这段代码但没有成功:
If Left(s, 8) = " In File" Then
Cells(i, "B").Offset(0, 1).Value = Split(",", s)(0)
End If
我有一个包含数百行的 .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
谢谢,但我还有一些问题,
我希望提取内容位于包含原始值的单元格前面,但事实并非如此:
enter image description here 因为我也需要提取以 "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
你可以使用 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()
函数的说明,请参阅:
尝试:
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
现在我想提取单元格内容的结尾如果开头 = "In File"
This is my cell
我试过这段代码但没有成功:
If Left(s, 8) = " In File" Then
Cells(i, "B").Offset(0, 1).Value = Split(",", s)(0)
End If