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

问题描述 投票:0回答:1

我昨天刚在这里问了一个问题(再次感谢!),但希望我再问一个问题就可以了。在我当前的VBA宏的最后,我有一个数组,里面有URLs链接。我试图搜索并替换一个所有URLs都通用的部分,但我的当前代码却出现了错误。这是我当前的Macro,没有添加部分,它的工作做得很好,并创建了一个数组,并在其中刮取URLs。

 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函数--我在 "subscript out of range "处得到一个 "subscript out of range "的错误。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,所以任何和所有的指点都将非常感激! 所以任何和所有的指点都将是非常感激的!谢谢! :) :)

html excel vba internet-explorer web-scraping
1个回答
0
投票

你已经创建了一个 零基一维 数组。 下面是一个改变该数组所有元素的例子。

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.