How to pass a cell (not its value) to a function as an argument?

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 10:41
Joined
Mar 22, 2009
Messages
1,034
Sub Proc()

DoSomethingWithThisCell(Range("A1"))

End Sub

Public function DoSomethingWithThisCell(CellToDoSomething as Range)

CellToDoSomething.SomeProperty = SomeValue

End function

P.S:
The very first time the cell is passed as range but after that only the value is getting passed. Why so? How to pass it as a range everytime?
 
Code:
[COLOR=#101094]Sub[/COLOR][COLOR=#303336] main[/COLOR][COLOR=#303336]()[/COLOR]
 [COLOR=#101094]Dim[/COLOR][COLOR=#303336] r [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Range ' This is an Excel Object     [/COLOR]
 [COLOR=#101094]With[/COLOR][COLOR=#303336] Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7d2727]"Sheet1"[/COLOR][COLOR=#303336])[/COLOR]
 [COLOR=#303336]   [/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] r [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336](.[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7d2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7d2727]1[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7d2727]3[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7d2727]4[/COLOR][COLOR=#303336])) ' cell 1 of a range[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] [/COLOR]
 [COLOR=#303336]    Debug[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Print r[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Address [/COLOR][COLOR=#858c93]' Open your debug window to see value[/COLOR][COLOR=#303336] [/COLOR]
 [COLOR=#303336]   select_cells r [/COLOR]
 [COLOR=#101094]End[/COLOR][COLOR=#101094]Sub[/COLOR]
 [COLOR=#101094][/COLOR] 
 [COLOR=#101094]Private[/COLOR][COLOR=#101094]Sub[/COLOR][COLOR=#303336] select_cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]aCell [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Range[/COLOR][COLOR=#303336])[/COLOR][COLOR=#858c93]'[/COLOR]
 [COLOR=#858c93] Activate the worksheet, so you can manually select a range on it[/COLOR][COLOR=#303336] [/COLOR]
 [COLOR=#303336]        aCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Worksheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]     [/COLOR]
 [COLOR=#303336]        aCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#101094]Select[/COLOR]
 [COLOR=#101094]End[/COLOR][COLOR=#101094]Sub[/COLOR]
 

Users who are viewing this thread

Back
Top Bottom