excel vba-提取2个字符之间的文本
如果我有此专栏:
ColA
-----
NUMBER(8,3)
NUMBER(20)
我需要一个可以运行的 VBA 函数(请注意,这些开始和结束字符串只会在单元格中出现一次):
extract_val(cell,start_str,end_str)
即。 extract_val(A1,"(",")") 并给出结果:
8,3
20
我只需要在其他 vba 代码中使用此函数,而不是将其作为公式放在工作表上。
更新(感谢答案,我决定了:)
---------------------------
Public Function extract_value(str As String) As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
On Error Resume Next
openPos = InStr(str, "(")
On Error Resume Next
closePos = InStr(str, ")")
On Error Resume Next
midBit = mid(str, openPos + 1, closePos - openPos - 1)
If openPos <> 0 And Len(midBit) > 0 Then
extract_value = midBit
Else
extract_value = "F"
End If
End Function
Public Sub test_value()
MsgBox extract_value("NUMBER(9)")
End Sub
If i had this column:
ColA
-----
NUMBER(8,3)
NUMBER(20)
I need a VBA function that would go (note these start and end string would only ever appear once in a cell):
extract_val(cell,start_str,end_str)
ie. extract_val(A1,"(",")") and give the results:
8,3
20
I only need to use this function within other vba code not by putting it as a formula on the sheet.
UPDATE (thanks to the answer, i settled on:)
---------------------------
Public Function extract_value(str As String) As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
On Error Resume Next
openPos = InStr(str, "(")
On Error Resume Next
closePos = InStr(str, ")")
On Error Resume Next
midBit = mid(str, openPos + 1, closePos - openPos - 1)
If openPos <> 0 And Len(midBit) > 0 Then
extract_value = midBit
Else
extract_value = "F"
End If
End Function
Public Sub test_value()
MsgBox extract_value("NUMBER(9)")
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
instr
来查找字符串中的字符(例如返回'('
的位置)。然后您可以使用mid
提取替换内容,使用'('
和')'
的位置(根据记忆):
您可能需要添加错误检查,以防这些字符出现在字符串。
You can use
instr
to locate a character within the string (returning the position of'('
for example). You can then usemid
to extract a substing, using the positions of'('
and')'
.Something like (from memory):
You may want to add error checking in case those characters don't occur in the string.
如果字符串是“Value of A is [1.0234] and Value of B is [3.2345]”
如果你想提取B的值即3.2345,那么
If the string is “Value of A is [1.0234] and Value of B is [3.2345]”
If you want to extract the value of B i.e., 3.2345, then
如果我有一个黄瓜表作为单元格中的测试数据,并且需要访问第四个和第五个管道之间“标题 1”下的值,下面是我的做法。我的表格示例如下,位于单元格 D7 中:
*单元格 D7:
访问“abcd”的代码,该代码在第 4 次出现“|”后找到并且在第 5 次出现“|”之前
If I have a cucumber table as test data in a cell, and need to access the value under 'Header 1' between 4th and 5th pipes, below is how I done it. My table example as below in cell D7 :
*Cell D7:
code to access 'abcd' which is found after 4th occurrence of '|' and before 5th occurrence of '|'
我知道这个问题已被标记为已回答,但我想我会根据更新的问题的功能添加我的代码。我创建了一个函数,可以让它解析多个。
I know this question has been marked answered, but i thought i will add my code based on the updated question's function. I made a function that will allow it to parse out multiple.