如果我有这个专栏:
ColA
-----
NUMBER(8,3)
NUMBER(20)
我需要一个VBA函数(注意这些开始和结束字符串只会在单元格中出现一次):
extract_val(细胞,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
答案 0 :(得分:19)
您可以使用instr
在字符串中查找字符(例如,返回'('
的位置)。然后,您可以使用mid
使用'('
和')'
的位置提取子位。
像(来自记忆):
dim str as string
dim openPos as integer
dim closePos as integer
dim midBit as string
str = "NUMBER(8,3)"
openPos = instr (str, "(")
closePos = instr (str, ")")
midBit = mid (str, openPos+1, closePos - openPos - 1)
如果字符串中没有出现这些字符,您可能需要添加错误检查。
答案 1 :(得分:1)
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
firstDelPos = InStrRev(textline, “[“) ‘ position of start delimiter
secondDelPos = InStrRev(textline, “]”) ‘ position of end delimiter
stringBwDels = Mid(textline, firstDelPos + 1, secondDelPos – firstDelPos – 1) ‘ extract the string between two delimiters
MsgBox (stringBwDels) ‘ message shows string between two delimiters
答案 2 :(得分:0)
我知道此问题已被标记为已回答,但是我想我将根据更新后的问题的功能添加代码。我做了一个函数,可以解析多个函数。
Sub testextract()
s = extract_values("This is [what i want to keep][And this]")
Debug.Print (s)
End Sub
Function extract_values(str As String, Optional openStr = "[", Optional closeStr = "]") As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
prevOpen = 1
prevClose = 1
Dim keep As String
While prevOpen <> 0
On Error Resume Next
openPos = InStr(prevOpen, str, openStr)
On Error Resume Next
closePos = InStr(prevClose, str, closeStr)
On Error Resume Next
midBit = Mid(str, openPos + 1, closePos - openPos - 1)
If openPos <> 0 And Len(midBit) > 0 Then
keep = keep & openStr & midBit & closeStr
End If
If openPos = 0 Or prevClose = 0 Then
i = 0
Else
i = 1
End If
prevOpen = openPos + i
prevClose = closePos + i
Wend
extract_values = keep
End Function
答案 3 :(得分:-1)
如果我在一个单元格中有一个黄瓜表作为测试数据,并且需要在第4个管道和第5个管道之间访问“ Header 1”(标题1)下的值,则下面是我的处理方法。我的表格示例在单元格D7中如下所示:
*单元格D7:
代码以访问在'|'第4次出现后找到的'abcd'并且在第5次出现“ |”之前
Dim sheet5 As Worksheet
Dim i As Integer
Dim k As Integer
Dim openPos As Long
Dim clsPos As Long
Dim textBetween as String
'Using for loop to find 4th occurrence of pipe '|' for openPos
For i = 1 To 4
openPos = InStr(openPos + 1, sheet5.Range("D7"), "|", vbTextCompare)
Next i
'Using for loop to find 5th occurrence of pipe '|' for clsPos
For k = 1 To 5
clsPos = InStr(clsPos + 1, sheet5.Range("D7"), "|", vbTextCompare)
Next k
'Displaying the value between openPos and clsPos
txtBetween = Mid(sheet5.Range("D7").Value, openPos + 1, clsPos - openPos - 1)
MsgBox ("Current Header 1 value: " & txtBetween)