有没有办法在同一个For Each循环中逐步遍历一个以上的单元格区域?

时间:2019-05-05 19:48:13

标签: excel vba

我当前正在尝试创建一个循环访问2个数据范围的循环。第一个范围是b16-b35,下一个范围是j16-j35。目前,我只能让2个循环中的1个循环进行。

我从一个For While循环开始。将i用作16-35的变量。当我尝试这种方法时,我无法获取msgbox来打印数据。我移至For每个循环。这使我能够单步执行一个单元格,而不能跨另一个单元格。

If [D8] = 2 Then

    Dim r As Range
    Dim j As Range
    Dim jcell As Range
    Dim cell As Range
    Set r = Range("B16:B35")
    Set j = Range("J16:J35")
    For Each cell In r
    For Each hcell In j
        If cell = "" Or cell = "N/A" Then GoTo ENDGAME

        MsgBox "pn is " & cell & " route is " & jcell

Next jcell

Next cell

ENDGAME:

End IF

当前方法使循环为每个r逐步遍历所有J。我尝试将for循环与and语句结合使用,从而使代码出错。

4 个答案:

答案 0 :(得分:0)

似乎确实有一个循环(进程),只是您的数据感觉在两个不同的位置。让我们遍历B16:B35,同时参考J列中的相应值:

Sub looper()

    Dim r As Range
    Dim cell As Range

    If [D8] = 2 Then

        Set r = Range("B16:B35")

        For Each cell In r

            If cell = "" Or cell = "N/A" Then GoTo ENDGAME

            MsgBox "pn is " & cell & " route is " & cell(1, 9)

        Next cell

ENDGAME:

    End If

End Sub

所以cell是范围对象,从B16开始...您可以通过其 offset 从范围对象引用另一个单元格... cell(1,9)表示以cell为例,看同一行(1),但第9列(将B列视为“一”,C列视为两; J列为九)。

在子程序顶部声明变量通常是个好主意,这就是为什么我移动了Dims的原因。严格来说,此代码无法正常工作。

答案 1 :(得分:0)

使用一个计数器作为For循环,并使用该计数器在每个范围内设置一个引用

Dim r As Range
Dim j As Range
Dim jcell As Range
Dim rcell As Range
Dim i as Long
Set r = Range("B16:B35")
Set j = Range("J16:J35")
For i = 1 to r.Rows.Count
    Set rcell = r.Cells(i, 1)
    Set jcell = j.Cells(i, 1)
    MsgBox "pn is " & rcell.Address & " route is " & jcell.Address
Next i

答案 2 :(得分:0)

不确定要执行的操作,但是以下操作应该可以执行您想要的操作。.

顺便说一句,将单元格定义为范围等不是最佳实践。最好给它起一个除函数等名称之外的名称。

    with thisworkbook.sheets(1)
        if .range("B8").value = 2 then
            for i = 16 to 35
                if .range("B" & i).value = "" or .range("B" & i).value = "N/A" then
                    goto EndGame
                else
                    msgbox "pn is " & .range("B" & i).value & " route is " & .range("J" & i).value
                end if
            next i
        EndGame:
        end if
    end with

如果要执行2个循环,首先要对B进行循环,而不要对J进行循环。但是,如果循环之一中的单元格之一不包含任何内容,或者n / a->函数将停止。如果您想转到下一个(i);迭代。您应该输入:

EndGame:

就在之前:

next i

-

    dim First_Range_Done as boolean

    with thisworkbook.sheets(1)
        if .range("B8").value = 2 then

            for i = 16 to 35

                if First_Range_Done = false then
                    if .range("B" & i).value = "" or .range("B" & i).value = "N/A" then
                        goto EndGame
                    else
                        msgbox "pn is " & .range("B" & i).value & " route is " & .range("J" & i).value
                    end if
                end if

                if First_Range_Done = true
                    if .range("J" & i).value = "" or .range("J" & i).value = "N/A" then
                        goto EndGame
                    else
                        msgbox "pn is " & .range("B" & i).value & " route is " & .range("J" & i).value
                    end if

                    if i = 35 then exit sub
                end if

                if i = 35 then
                    First_Range_Done = true
                    i = 15
                end if

            next i

        EndGame:
        end if
    end with

答案 3 :(得分:-1)

Dim r1 As Range
Dim r2 As Range
Dim u As Range
Dim res As String
Set r1 = Range("A1:B1")
Set r2 = Range("C3:D3")
Set u = Union(r1,r2)
res = ""
For Each cell In u
   res = res + cell.Value2
Next cell
MsgBox res

假定单元格具有以下值:

-------------------
| Address | Value | 
-------------------
| A1      | a1    |
| B1      | b1    |
| C3      | c3    |
| D3      | d3    |
-------------------

您将得到a1b1c3d3所显示的结果MsgBox

使用这种方法,您可以获得更多好处,可以组合不同维度的范围。