Sum innertext VBA IE

时间:2015-04-25 08:45:15

标签: excel vba internet-explorer

我得到了这几个数字,我想提供的总和,而不是后来总结它

MsgBox ie.document.getElementsByClassName("post_click_or_post_view_revenue")(0).innertext
MsgBox ie.document.getElementsByClassName("post_click_or_post_view_revenue")(1).innertext
MsgBox ie.document.getElementsByClassName("post_click_or_post_view_revenue")(2).innertext
MsgBox ie.document.getElementsByClassName("post_click_or_post_view_revenue")(3).innertext

我想做这样的事情

MsgBox Sum(ie.document.getElementsByClassName("post_click_or_post_view_revenue").innertext)

这可能吗?

1 个答案:

答案 0 :(得分:1)

您必须循环浏览与您的类匹配的一系列标记,并在您进行过程中将.innerText转换为实际数字(如果仍然不是数字,则看起来像数字的文本)。

dim dTotal as double, l as long, e as long
with ie.document
    l = .getElementsByClassName("post_click_or_post_view_revenue").length - 1
    for e = 0 to l
        if isnumeric(trim(.getElementsByClassName("post_click_or_post_view_revenue")(e).innertext)) then _
            dTotal = dTotal + CDbl(.getElementsByClassName("post_click_or_post_view_revenue")(e).innertext)
    next e
end with
msgbox "The total is " & dTotal

现在,这将循环遍历具有匹配类名的所有元素。如果您只想从0到3,请使用for e = 0 to 3。在进入循环要求全部四个之前,确保实际上有四个。