Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

Excel VBA replacing question mark in a string

I have a string ShrtDesc as follows:

? HYPOALLERGENIdisorders. SET OF 3. 
? SAVE MONEY & 

I am trying the following:

ShrtDesc = Replace(ShrtDesc, "?", "")
ShrtDesc = Replace(ShrtDesc, "~?", "")
ShrtDesc = Replace(ShrtDesc, Chr(63), "")
ShrtDesc = Replace(ShrtDesc, ChrW(63), "")

But I am unable to replace the question mark. I am not sure what else to try.

Edit: I got it by scraping an Amazon page.

Debug.Print Asc(Left(ShrtDesc, 1))
63

Debug.Print VarType(ShrtDesc)
 8 

Edit 2: I am using the following code:

Sub testSub()
    Dim pKey As String
    pKey = "B07R212XKH"
    Dim AmazonUrl As String
    AmazonUrl = "https://www.amazon.com/s?k=" & Trim(pKey)
    
    Dim Msxml, Msxml2, Doc, Doc2 As Object
    Set Msxml = CreateObject("Microsoft.xmlhttp")
    Set Doc = CreateObject("htmlfile")
    Set Msxml2 = CreateObject("Microsoft.xmlhttp")
    Set Doc2 = CreateObject("htmlfile")
    Msxml.Open "GET", AmazonUrl, False
    Msxml.Send ""
    Doc.body.innerhtml = Msxml.ResponseText
    
    Dim AllProducts, Product As Object
    Dim ProductUrl, ShrtDesc As String
    
    Set AllProducts = Doc.getelementsbytagname("h2")
    For Each Product In AllProducts
        ProductUrl = Product.getelementsbytagname("a").Item(0).href
        ProductUrl = Replace(ProductUrl, "about:/", "https://www.amazon.com/")
        
        Msxml2.Open "GET", ProductUrl, False
        Msxml2.Send ""
        Doc2.body.innerhtml = Msxml2.ResponseText
        ShrtDesc = Doc2.getelementbyid("featurebullets_feature_div").innertext
        ShrtDesc = Replace(ShrtDesc, "?", "")
        Next
End Sub
question from:https://stackoverflow.com/questions/65599107/excel-vba-replacing-question-mark-in-a-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Right, so as per @ScottHoltzman; The question mark can be a placeholder for text not displayed correctly by Excel. So let's take a look at the text as is at the source:

enter image description here

Right, that's not an question mark indeed, but instead its the black heart suit. The unicode for that character would be: "U+2665" so you can now use:

ShrtDesc = Replace(ShrtDesc, ChrW(&H2665), "")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...