Loop through each row of a range in Excel

前端 未结 4 1849
闹比i
闹比i 2020-11-28 19:01

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.

4条回答
  •  悲哀的现实
    2020-11-28 19:27

    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).

    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.

提交回复
热议问题