Excel VBA Using wildcard to replace string within string

穿精又带淫゛_ 提交于 2020-01-05 08:02:12

问题


I have a difficult situation and so far no luck in finding a solution.

My VBA collects number figures like $80,000.50. and I'm trying to get VBA to remove the last period to make it look like $80,000.50 but without using right().

The problem is after the last period there are hidden spaces or characters which will be a whole lot of new issue to handle so I'm just looking for something like:

replace("$80,000.50.",".**.",".**")

Is this possible in VBA?


回答1:


I cant leave a comment so....

what about InStrRev?

Private Sub this()
    Dim this As String
    this = "$80,000.50."
    this = Left(this, InStrRev(this, ".") - 1)
    Debug.Print ; this
End Sub



回答2:


Mid + Find

You can use Mid and Find functions. Like so:

The Find will find the first dot . character. If all the values you are collecting are currency with 2 decimals, stored as text, this will work well.

The formula is: =MID(A2,1,FIND(".",A2)+2)

VBA solution

Function getStringToFirstOccurence(inputUser As String, FindWhat As String) As String
    getStringToFirstOccurence = Mid(inputUser, 1, WorksheetFunction.Find(FindWhat, inputUser) + 2)
End Function

Other possible solutions, hints

Trim + Clear + Substitute(Char(160)): Chandoo - Untrimmable Spaces – Excel Formula
Ultimately, you can implement Regular expressions into Excel UDF: VBScript’s Regular Expression Support




回答3:


How about:

Sub dural()
    Dim r As Range
    For Each r In Selection
        s = r.Text
        l = Len(s)
        For i = l To 1 Step -1
            If Mid(s, i, 1) = "." Then
                r.Value = Mid(s, 1, i - 1) & Mid(s, i + 1)
                Exit For
            End If
        Next i
    Next r
End Sub

This will remove the last period and leave all the other characters intact. Before:

and after:

EDIT#1:

This version does not require looping over the characters in the cell:

Sub qwerty()
    Dim r As Range

    For Each r In Selection
        If InStr(r.Value, ".") > 0 Then r.Characters(InStrRev(r.Text, "."), 1).Delete
    Next r
End Sub



回答4:


Shortest Solution

Simply use the Val command. I assume this is meant to be a numerical figure anyway? Get rid of commas and the dollar sign, then convert to value, which will ignore the second point and any other trailing characters! Robustness not tested, but seems to work...

Dim myString as String
myString = "$80,000.50.   junk characters     "

' Remove commas and dollar signs, then convert to value.
Dim myVal as Double
myVal = Val(Replace(Replace(myString,"$",""),",",""))

' >> myVal = 80000.5

' If you're really set on getting a formatted string back, use Format:
myString = Format(myVal, "$000,000.00")

' >> myString = $80,000.50

From the Documentation,

The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized.

This is why we must first remove the dollar sign, and why it ignores all the junk after the second dot, or for that matter anything non numerical at the end!

Working with Strings

Edit: I wrote this solution first but now think the above method is more comprehensive and shorter - left here for completeness.

Trim() removes whitespace at the end of a string. Then you could simply use Left() to get rid of the last point...

' String with trailing spaces and a final dot
Dim myString as String
myString = "$80,000.50.    "

' Get rid of whitespace at end
myString = Trim(myString)

' Might as well check if there is a final dot before removing it
If Right(myString, 1) = "." Then

    myString = Left(myString, Len(myString) - 1)

End If

' >> myString = "$80,000.50"


来源:https://stackoverflow.com/questions/41618327/excel-vba-using-wildcard-to-replace-string-within-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!