在Excel中遍历范围的每一行

时间:2021-02-22 12:14:19

This is one of those things that I'm sure there's a built-in function for (and I may well have been told it in the past), but I'm scratching my head to remember it.

这是我确信有一个内置函数的东西(我可能在过去被告知过),但我正绞尽脑汁去记住它。

How do I loop through each row of a multi-column range using Excel VBA? All the tutorials I've been searching up seem only to mention working through a one-dimensional range...

如何使用Excel VBA对多列范围内的每一行进行循环?我搜索过的所有教程似乎只提到了在一维范围内工作……

4 个解决方案

#1


120  

Dim a As Range, b As Range

Set a = Selection

For Each b In a.Rows
    MsgBox b.Address
Next

#2


120  

Something like this:

是这样的:

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("A1:C2")

For Each row In rng.Rows
  For Each cell in row.Cells
    'Do Something
  Next cell
Next row

#3


4  

In Loops, I always prefer to use the Cells class, using the R1C1 reference method, like this:

在循环中,我总是喜欢使用单元格类,使用R1C1引用方法,如下所示:

Cells(rr, col).Formula = ...

This allows me to quickly and easily loop over a Range of cells easily:

这使我能够快速、轻松地在一系列单元格上循环:

Dim r As Long
Dim c As Long

c = GetTargetColumn() ' Or you could just set this manually, like: c = 1

With Sheet1 ' <-- You should always qualify a range with a sheet!

    For r = 1 To 10 ' Or 1 To (Ubound(MyListOfStuff) + 1)

        ' Here we're looping over all the cells in rows 1 to 10, in Column "c"
        .Cells(r, c).Value = MyListOfStuff(r)

        '---- or ----

        '...to easily copy from one place to another (even with an offset of rows and columns)
        .Cells(r, c).Value = Sheet2.Cells(r + 3, 17).Value


    Next r

End With

#4


4  

Just stumbled upon this and thought I would suggest my solution. I typically like to use the built in functionality of assigning a range to an multi-dim array (I guess it's also the JS Programmer in me).

我无意中发现了这一点,并认为我应该提出我的解决方案。我通常喜欢使用内置的功能,为一个多dim数组分配范围(我猜我里面也是JS程序员)。

I frequently write code like this:

我经常这样写代码:

Sub arrayBuilder()

myarray = Range("A1:D4")

'unlike most VBA Arrays, this array doesn't need to be declared and will be automatically dimensioned

For i = 1 To UBound(myarray)

    For j = 1 To UBound(myarray, 2)

    Debug.Print (myarray(i, j))

    Next j

Next i

End Sub

Assigning ranges to variables is a very powerful way to manipulate data in VBA.

为变量分配范围是在VBA中操作数据的一种非常强大的方法。

#1


120  

Dim a As Range, b As Range

Set a = Selection

For Each b In a.Rows
    MsgBox b.Address
Next

#2


120  

Something like this:

是这样的:

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("A1:C2")

For Each row In rng.Rows
  For Each cell in row.Cells
    'Do Something
  Next cell
Next row

#3


4  

In Loops, I always prefer to use the Cells class, using the R1C1 reference method, like this:

在循环中,我总是喜欢使用单元格类,使用R1C1引用方法,如下所示:

Cells(rr, col).Formula = ...

This allows me to quickly and easily loop over a Range of cells easily:

这使我能够快速、轻松地在一系列单元格上循环:

Dim r As Long
Dim c As Long

c = GetTargetColumn() ' Or you could just set this manually, like: c = 1

With Sheet1 ' <-- You should always qualify a range with a sheet!

    For r = 1 To 10 ' Or 1 To (Ubound(MyListOfStuff) + 1)

        ' Here we're looping over all the cells in rows 1 to 10, in Column "c"
        .Cells(r, c).Value = MyListOfStuff(r)

        '---- or ----

        '...to easily copy from one place to another (even with an offset of rows and columns)
        .Cells(r, c).Value = Sheet2.Cells(r + 3, 17).Value


    Next r

End With

#4


4  

Just stumbled upon this and thought I would suggest my solution. I typically like to use the built in functionality of assigning a range to an multi-dim array (I guess it's also the JS Programmer in me).

我无意中发现了这一点,并认为我应该提出我的解决方案。我通常喜欢使用内置的功能,为一个多dim数组分配范围(我猜我里面也是JS程序员)。

I frequently write code like this:

我经常这样写代码:

Sub arrayBuilder()

myarray = Range("A1:D4")

'unlike most VBA Arrays, this array doesn't need to be declared and will be automatically dimensioned

For i = 1 To UBound(myarray)

    For j = 1 To UBound(myarray, 2)

    Debug.Print (myarray(i, j))

    Next j

Next i

End Sub

Assigning ranges to variables is a very powerful way to manipulate data in VBA.

为变量分配范围是在VBA中操作数据的一种非常强大的方法。