VBA 对于每个循环不循环我想要的方式



我有以下循环

r = 4
c = 4
Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList/item[recordType='TPI']/*[not(self::catch or self::reprive or self::cate or self::reet or self::aarg or self::crane)]")
For a_counter = 1 To 7
For Each userbean In userBeanList
Sheets("Sheet2").Cells(r, c) = userbean.nodeName
Sheets("Sheet2").Cells(r + 1, c) = userbean.Text
 r = r + 2
a_counter = a_counter + 1
If (a_counter = 7) Then Exit For
Next userbean
c = c + 1
r = 4
a_counter = 1
Next a_counter

基本上是遍历我的节点,在第 4 行打印节点名称,然后在第 5 行下方的单元格中打印节点值,这重复 6 次,以便我在电子表格的列表中获得节点名称后跟节点值。然后我有一个计数器,当它达到 7 退出时,for 循环将列增加 1 将行重置回 4,然后重复。但是,userBeanList 中的每个用户 bean 似乎将自己重置回起点,因此它不会再次获取下一个用户 bean,我如何修改下面的代码以获取节点中的下一个用户 bean?

试试这个:

r = 4
c = 4
a_counter = 1
Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList/item[recordType='TPI']/*[not(self::catch or self::reprive or self::cate or self::reet or self::aarg or self::crane)]")
For Each userbean In userBeanList
    Sheets("Sheet2").Cells(r, c) = userbean.nodeName
    Sheets("Sheet2").Cells(r + 1, c) = userbean.Text
    r = r + 2
    a_counter = a_counter + 1
    If a_counter  >= 7 Then
        c = c + 1
        r = 4
        a_counter = 1
    End If
Next userbean

您正在尝试将a_counter用作 for 循环,并且作为单独的计数器,您不需要 for 循环


编辑:

你真的根本不需要a-counter。 您可以使用 R 值检查何时完成 6:

r = 4
c = 4
Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList/item[recordType='TPI']/*[not(self::catch or self::reprive or self::cate or self::reet or self::aarg or self::crane)]")
For Each userbean In userBeanList
    Sheets("Sheet2").Cells(r, c) = userbean.nodeName
    Sheets("Sheet2").Cells(r + 1, c) = userbean.Text
    r = r + 2
    If r  > 14 Then
        c = c + 1
        r = 4
    End If
Next userbean

相关内容

  • 没有找到相关文章

最新更新