Passing a Cell In a Range as An Object

JStellato

Board Regular
Joined
Nov 6, 2010
Messages
55
I'm trying to refactor some code, and got stuck when trying to refactor a section that works on cells

Code:
Public Sub MainSub ()

     MyArray = Range([A1], [B10])

     For lngRow = 1 To UBound(MyArray, 1)

[COLOR=#008000]          ' Currently I can do this with no problem
          ' Cell.Offset(, 6).AddComment
          ' Cell.Offset(, 6).Comment.Text = "Comment"
          ' What I want to do is [/COLOR]

          Refactored(Cell.Offset(, 6))

     Next

End Sub

Public Sub Refactored(Cell as Cell)
     Cell.AddComment
     Cell.Comment.Text = "Comment"
End Sub

Is this possible?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I don't quite understand, I just want to pass the cell I'm working with, in this case the cell does exist....

EDIT

Ok I see what you are saying, I sanitized this, that's why. However with the variable cell assigned I still can't refactor this.
 
Last edited:
Upvote 0
I'm trying to refactor some code, and got stuck when trying to refactor a section that works on cells

Code:
Public Sub MainSub ()

     MyArray = Range([A1], [B10])

     For lngRow = 1 To UBound(MyArray, 1)

[COLOR=#008000]          ' Currently I can do this with no problem
          ' Cell.Offset(, 6).AddComment
          ' Cell.Offset(, 6).Comment.Text = "Comment"
          ' What I want to do is [/COLOR]

          Refactored(Cell.Offset(lngRow, 6))

     Next

End Sub

Public Sub Refactored(Cell as Cell)
     Cell.AddComment
     Cell.Comment.Text = "Comment"
End Sub

Is this possible?

Fixed missing variable, but refactoring this still fails
 
Upvote 0
Hi there,

Put 'Option Explicit' at the very top of the module. This will cause errors to show right away.

For instance: Public Sub Refactored(Cell as Cell) will raise a 'User defined type not defined' error, as Cell is not defined. In this case, you wanted as Range.

Hope that helps,

Mark
 
Upvote 0
In MainSub,
Cell.Offset(lngRow,6) is the cell lngRow down and 6 to the right of the cell referened by the variable Cell.


If you want the cell in row lngRow of column F, the syntax would be Cells(lngRow, 6)
Perhaps
Code:
Public Sub MainSub ()

     MyArray = Range([A1], [B10])

     For lngRow = 1 To UBound(MyArray, 1)

          Call Refactored(ActiveSheet.Cells(lngRow, 6))

     Next

End Sub
 
Upvote 0
Thanks for the input, I did get this working because you put me on the right track. Here is the working code

Code:
Public Sub MainSub()

Set DataRange = Range(Cells(1, 1), Cells(10, 1))

For Each Cell In DataRange

     Call Refactored(Cell.Offset(, 6))

Next

End Sub


Public Sub Refactored(cellObj As Range)
     cellObj.AddComment
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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