VBA Differences

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,180
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
if r =("A1:M1") then
I know what this would do
For each c in r

But what would be the difference between the following below:

For each c in r.rows
For each c in r.cells
For each c in r.columns
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Use the code below to explore the differences. Basically, Each c in r and Each c in r.Cells are identical. Each c in r.rows and Each c in r.columns will typically generate errors, except for the unique cases where r is either a single row or a single column. In those cases, For each c in r.columns is identical to For Each c in r, and For each c in r.rows is identical to For each c in r, respectively.
Code:
Sub try()
Dim r As Range, c As Range
Set r = Range("A1:M1")
For Each c In r   'substitute r.rows, r.cells and r.columns for r
    MsgBox c.Value
Next c
End Sub
 
Upvote 0
Thanks JoeMo! I appreciate your help on this topic. Thanks again!
 
Upvote 0
if r =("A1:M1") then
I know what this would do
For each c in r

But what would be the difference between the following below:

For each c in r.cells --- For each cell in the range.
For each c in r.rows --- For each row in the range.
For each c in r.columns --- For each column in the range.

Examples:

Code:
Sub [COLOR=#0000ff]test_cell[/COLOR]()
  Dim r As Range, c As Range
  Set r = Range("A1:D5")
  For Each c In [COLOR=#0000ff]r.Cells[/COLOR] 'or r
      MsgBox c.[COLOR=#0000ff]Address[/COLOR]
  Next
End Sub


Sub [COLOR=#008000]test_row[/COLOR]()
  Dim r As Range, c As Range
  Set r = Range("A1:D5")
  For Each c In [COLOR=#008000]r.Rows[/COLOR]
      MsgBox c.[COLOR=#008000]Row[/COLOR]
  Next
End Sub


Sub [COLOR=#ff0000]test_column[/COLOR]()
  Dim r As Range, c As Range
  Set r = Range("A1:D5")
  For Each c In [COLOR=#ff0000]r.Columns[/COLOR]
      MsgBox c.[COLOR=#ff0000]Column[/COLOR]
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top