excel vba-提取2个字符之间的文本

发布于 2024-12-02 22:42:48 字数 854 浏览 0 评论 0原文

如果我有此专栏:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨笨の傻瓜 2024-12-09 22:42:48

您可以使用 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)

您可能需要添加错误检查,以防这些字符出现在字符串。

You can use instr to locate a character within the string (returning the position of '(' for example). You can then use mid to extract a substing, using the positions of '(' and ')'.

Something like (from memory):

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)

You may want to add error checking in case those characters don't occur in the string.

缱倦旧时光 2024-12-09 22:42:48

如果字符串是“Value of A is [1.0234] and Value of B is [3.2345]”

如果你想提取B的值即3.2345,那么

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

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
少女净妖师 2024-12-09 22:42:48

如果我有一个黄瓜表作为单元格中的测试数据,并且需要访问第四个和第五个管道之间“标题 1”下的值,下面是我的做法。我的表格示例如下,位于单元格 D7 中:

*单元格 D7:

在此处输入图像描述

访问“abcd”的代码,该代码在第 4 次出现“|”后找到并且在第 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)

输入图片此处描述

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:

enter image description here

code to access 'abcd' which is found after 4th occurrence of '|' and before 5th occurrence of '|'

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)

enter image description here

客…行舟 2024-12-09 22:42:48

我知道这个问题已被标记为已回答,但我想我会根据更新的问题的功能添加我的代码。我创建了一个函数,可以让它解析多个。

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

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文