如何搜索和替换 VBA 数组中的值?

How can I search and replace values in a VBA array?

我昨天刚在这里问了一个问题(再次感谢!)但希望我问另一个问题没问题。 在当前 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

这就是我的问题所在。该数组中的所有 URLs/values 都包含 "old.reddit.com",我正在尝试将其替换为 "removeddit.com"。我已将此代码添加到其中,但它没有完成工作,即使据我了解,我正确使用了 Replace 函数 - 我在 url(rw, col) = Replace 行收到 "subscript out of range" 错误。

出现错误的部分是第一个。 (第二部分应该使用与原始 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,我觉得我正在尝试的东西太高级了,所以任何和所有的指示都将不胜感激! 谢谢! :)

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

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