簡體   English   中英

如何從單元格中的文本解析值並將值放置到其他單元格中

[英]How to parse values from text in a cell and place values into other cells

我在N2欄中有一個帶有以下文本的單元格。

71sdfsdf 2230400DATE of join 1997-06-03PROGRAMME : ACCES PLUSNew Value 534.55Right value 534.55Clear Value 308.50

我需要在這種情況下將“ New Value之后的數字534.55保存在O2列中,在P2列中再次將“ Right Value值”旁邊的值保存為534.55,如果存在“ Clear Value ,則它旁邊的值就是308.50。保存在第二季度。

Clear value不會始終包含在內。

我們可以為此編寫一個宏嗎? 我已經使用InStr來獲取單元格中存在的值,但是我不知道宏中的光標位置如何工作。

如果您已經在使用InStr函數,則只需將提取的值放入所需的單元格中即可:

Public Sub YourMethod()

    Dim s As String
    s = "71sdfsdf 2230400DATE of join 1997-06-03PROGRAMME : ACCES PLUSNew Value 534.55Right value 534.56Clear Value 308.50"

    Const NEW_VALUE = "New Value"
    Const RIGHT_VALUE = "Right value"
    Const CLEAR_VALUE = "Clear Value"

    Dim posNewValue As Integer
    posNewValue = InStr(1, s, NEW_VALUE, VbCompareMethod.vbTextCompare)

    Dim posRightValue As Integer
    posRightValue = InStr(posNewValue, s, RIGHT_VALUE, VbCompareMethod.vbTextCompare)

    Dim posClearValue As Integer
    posClearValue = InStr(posRightValue, s, CLEAR_VALUE, VbCompareMethod.vbTextCompare)

    Dim newValue As String
    posNewValue = posNewValue + Len(NEW_VALUE)
    newValue = Mid$(s, posNewValue, posRightValue - posNewValue)

    Dim rightValue As String
    Dim clearValue As String
    posRightValue = posRightValue + Len(RIGHT_VALUE)
    If posClearValue > 0 Then
        rightValue = Mid$(s, posRightValue, posClearValue - posRightValue)
        posClearValue = posClearValue + Len(CLEAR_VALUE)
        clearValue = Mid$(s, posClearValue)
    Else
        rightValue = Mid$(s, posRightValue)
    End If

    Sheets("Sheet1").Range("O2").Value = newValue
    Sheets("Sheet1").Range("P2").Value = rightValue
    Sheets("Sheet1").Range("Q2").Value = clearValue


End Sub

如果數字的長度是允許的,則可以使用以下方法:

For i = MyStart To MyLimit 'rows of the range of interest
    If InStr(1, "New Value ", Range("N" & i).Value) <> 0 Then _
        Range("O" & i).Value = Mid(Range("N" & i).Value, InStr(1, "New Value ", Range("N" & i).Value), 6)
    If InStr(1, "Right Value ", Range("N" & i).Value) <> 0 Then _
        Range("P" & i).Value = Mid(Range("N" & i).Value, InStr(1, "Right Value ", Range("N" & i).Value), 6)
    If InStr(1, "Clear Value ", Range("N" & i).Value) <> 0 Then _
        Range("Q" & i).Value = Mid(Range("N" & i).Value, InStr(1, "Clear Value ", Range("N" & i).Value), 6)
Next i

注意是否存在空格(我想它們是存在的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM