简体   繁体   English

Excel VBA 替换字符串中的问号

[英]Excel VBA replacing question mark in a string

I have a string ShrtDesc as follows:我有一个字符串ShrtDesc如下:

? 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:编辑2:我正在使用以下代码:

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

Right, so as per @ScottHoltzman;对,正如@ScottHoltzman 所说; The question mark can be a placeholder for text not displayed correctly by Excel.问号可以是 Excel 未正确显示的文本的占位符。 So let's take a look at the text as is at the source :因此,让我们按原样查看原文

在此处输入图像描述

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:该字符的 unicode 将是:“U+2665”,因此您现在可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM