【问题标题】:How can I search and replace values in a VBA array?如何搜索和替换 VBA 数组中的值?
【发布时间】:2020-08-20 06:28:16
【问题描述】:

我昨天刚刚在这里问了一个问题(再次感谢!),但希望我可以问另一个问题。 在我当前的 VBA 宏的末尾,我有一个包含 URL / 链接的数组。我正在尝试搜索和替换所有 URL 共有的部分,但我当前的代码出错了。 这是我当前没有添加部分的宏,它的工作做得很好,并创建了一个数组并抓取其中的 URL。

 Sub GetData()

    Dim IE As InternetExplorer
    Dim itemEle As Object
    Dim upvote As Integer, awards As Integer, animated As Integer
    Dim postdate As String, upvotepercent As String, oc As String, filetype As String, linkurl As String, myhtmldata As String, visiComments As String, totalComments As String, removedComments As String
    Dim y As Integer

    Set IE = New InternetExplorer
    IE.Visible = False

    IE.navigate (ActiveCell.Value)
    Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop

    Dim nodeList As Object, i As Long, urls(), results(), results2()

    Set nodeList = IE.document.querySelectorAll(".comments")
    ReDim urls(0 To nodeList.Length - 1)
    ReDim results(1 To nodeList.Length, 1 To 5)
    'Store all urls in an array to later loop
    For i = 0 To nodeList.Length - 1
        urls(i) = nodeList.Item(i).href
    Next

    For i = LBound(urls) To UBound(urls)
        IE.Navigate2 urls(i)    'opens in a new tab
        While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
        results(i + 1, 1) = IE.document.querySelector("a.title").innerText 'title
        results(i + 1, 2) = IE.document.querySelector(".number").innerText 'upvotes
        upvotepercent = IE.document.querySelector(".word").NextSibling.NodeValue  '%
        results(i + 1, 3) = CDbl(Mid(upvotepercent, 3, 2)) / 100
        results(i + 1, 4) = IE.document.querySelector("div.date > time").innerText
    Next

这就是我的问题所在。该数组中的所有 URL/值都包含“old.reddit.com”,我正在尝试将其替换为“removeddit.com”。我已将这段代码添加到其中,尽管据我了解,我正确使用了 Replace 函数 - 我在 url(rw, col) = Replace 行收到“下标超出范围”错误。

给出错误的部分是第一个。 (第二部分应该使用与原始 URL 相同的方法从每个新 URL 中抓取两个值,但现在第一个是问题 - 或者我认为,有时调试器不是很清楚)。

tofind = "old.reddit.com"
toreplace = "removeddit.com"
For rw = LBound(urls) To UBound(urls)
    For col = LBound(urls, 1) To UBound(urls, 1)
        urls(rw, col) = Replace(urls(rw, col), tofind, toreplace)
    Next
Next


For i = LBound(urls) To UBound(urls)
    IE.Navigate2 urls(i)    'opens in a new tab
    While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
    Application.Wait (Now + TimeValue("0:00:03"))
    visiComments = IE.document.querySelector(".removed-text").innerText 'title
    results(i + 1, 5) = visiComments
Next
ActiveSheet.Cells(1, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
IE.Quit
End Sub

考虑到我刚刚进入 VBA,我觉得我正在尝试一些太高级的东西,所以任何和所有的指针都将不胜感激! 谢谢! :)

【问题讨论】:

  • 不应该是For col = LBound(urls, 2) To UBound(urls, 2)吗?
  • 也就是说,urls 好像是一维数组?
  • 如果您的值是字符串并且您的数组是 1D 并且您需要查找的文本在 url 中是唯一的,那么您可以使用 ',' 加入分隔以获取单个字符串,替换以替换唯一的文本,然后在 "," 处拆分以获取已更新的 url 数组。

标签: html excel vba internet-explorer web-scraping


【解决方案1】:

您已经创建了一个从零开始的一维数组。这是更改该数组的所有元素的示例:

Sub dural()
    Dim url(0 To 2) As String

    Dim U As Long, L As Long, i As Long, a
    url(0) = "xLarry"
    url(1) = "nothing"
    url(2) = "111Larry"

    L = LBound(url)
    U = UBound(url)
    For i = L To U
        url(i) = Replace(url(i), "Larry", "Moe")
    Next i

    msg = ""
    For Each a In url
        msg = msg & vbCrLf & a
    Next a
    MsgBox msg
End Sub

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 2012-01-29
    • 2012-08-22
    • 2016-09-30
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2019-02-07
    相关资源
    最近更新 更多