Using Cells property to represent the active range

bearcub

Well-known Member
Joined
May 18, 2005
Messages
734
Office Version
  1. 365
  2. 2013
  3. 2010
  4. 2007
Platform
  1. Windows
I have the following VBA Code Snippet and I've trying to learn how to use the Cells object.

How would I replace the Activecell object with the Cells object in the Do Loop?

In this instance, the Active cell would be Cells(2,2).

What confuses me is how to replace the activecell object with the cells object in a Do while loop as opposed to a For next loop.

Code:
    Range("B2").Select

    'While the activecell isn't blank...

    Do While ActiveCell.Value <> ""
        
        'The logical test...
        'If the value of the activecell is "Male", AND column E DOES NOT EQUAL "Brown"...

        If ActiveCell.Value = "Male" And ActiveCell.Offset(0, 3).Value <> "Brown" Then
            
            '...then write "It's a brown haired MAN!" in column G

            ActiveCell.Offset(0, 5).Value = "It's a MAN...but he DOESN't have brown hair!"
        

        End If
    
    'Move down a cell
    ActiveCell.Offset(1, 0).Select

   Loop

Thank you for your help in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Look at this structure. It doesn't use the .Cells property, but it avoids Selection and use of ActiveCell

Code:
Dim oneCell as Range

Set oneCell = Range("B2")

Do while oneCell.Value <> ""
    If oneCell.Value = "Male" And OneCell.Offset(0,1).Value <> "Brown" Then
        oneCell.Offset(0,5).Value = "Its a MAN...but he Doesn't Have Brown hair"
    End IF
    Set oneCell = oneCell.Offset(1,0)
Loop
 
Upvote 0
How about
Code:
Sub bearcub()
   Dim i As Long
   
   Do While Cells(2, 2).Offset(i) <> ""
      With Cells(2, 2)
         If .Offset(i).Value = "Male" And .Offset(i, 3).Value <> "Brown" Then
            .Offset(i, 5).Value = "It's a MAN...but he DOESN't have brown hair!"
         End If
      End With
      i = i + 1
   Loop
End Sub
 
Last edited:
Upvote 0
Very interesting, they both work, thank you very much, Mike and Fluff

Fluff,
In you code, how does the row increment - is it the i variable in the Offset property (the Offset(i) piece - i represents the row?

Thank you both again for the tips, great stuff!

I take it then that using the Cells property in this instance isn't the best approach?
 
Upvote 0
Try this with for

Code:
Sub test1()
    For i = 2 To Range("B" & Rows.Count).End(xlUp).Row
        If Cells(i, "B").Value = "Male" And Cells(i, "E").Value <> "Brown" Then
            Cells(i, "G").Value = "It's a MAN...but he DOESN't have brown hair!"
        End If
    Next
End Sub

Try this too without loop

Code:
Sub test()
    With Range("G2:G" & Range("B" & Rows.Count).End(xlUp).Row)
        .FormulaR1C1 = "=IF(AND(RC[-5]=""Male"",RC[-2]<>""Brown""),""It's a MAN...but he DOESN't have brown hair!"","""")"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Try this too without loop

Code:
Sub test()
    With Range("G2:G" & Range("B" & Rows.Count).End(xlUp).Row)
        .FormulaR1C1 = "=IF(AND(RC[-5]=""Male"",RC[-2]<>""Brown""),""It's a MAN...but he DOESN't have brown hair!"","""")"
        .Value = .Value
    End With
End Sub
Another way without a loop...
Code:
[table="width: 500"]
[tr]
	[td]Sub MalesWithoutBrownHair()
  Dim LastRow As Long
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
  Range("G2:G" & LastRow) = Evaluate(Replace("IF((B2:B#=""Male"")*(E2:E#<>""Brown""),""It's a MAN... but he DOESN'T have brown hare!"","""")", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thank you Rich and Dante. How do these work with using a loop? How does the code go from the end of the sheet all the way up to the second row?

Really cool and mind blowing!

Michael
 
Upvote 0
Your welcome

What this line does is to cover the range from G2 to G(last row with data) and in that range it puts the formula with the conditions.

Code:
[COLOR=#0000ff]With Range("G2:G" & Range("B" & Rows.Count).End(xlUp).Row)[/COLOR]

Then put in that same range of cells the resulting value of the formula

Code:
[COLOR=#0000ff].Value = .Value[/COLOR]
 
Upvote 0
Thank you for the explanation. I was a little confused by the concatenation but once I looked at it closely I say that you were referencing the last cell on the spreadsheet.

Michael
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,115
Members
452,545
Latest member
boybenqn

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