PC Review


Reply
Thread Tools Rate Thread

Clearing Cells

 
 
plh
Guest
Posts: n/a
 
      30th Sep 2007
Hello Everyone,
I thought this would be easy, but I must have something fundamentally wrong.
The following function has various cell-clearing test code lines in it, but none
have any effect on the contents of the cell. I have tried many other ways to do
it besides what is below.

Function ClearIt(v As Double) As Boolean
ActiveWorkbook.ActiveSheet.Range("MyData").Select
Selection.ClearContents
ActiveWorkbook.ActiveSheet.Range("MyData").ClearContents
'Range("C5").Value = ""
ActiveWorkbook.ActiveSheet.Cells(5, 3).ClearContents
End Function

Changes to C5 fire the function as D5 contains =ClearIt(C5). My ultimate goal
is to have a change to any of the cells in the range "MyData" fire "ClearIt"
which will decide whether or not to clear the cells based on a set of logical
rules. But I am stuck on what I thought was a very simple test! Can anyone help
me?
Thanx,
-plh


--
Where are we going and why am I in this HAND BASKET??
 
Reply With Quote
 
 
 
 
Gord Dibben
Guest
Posts: n/a
 
      30th Sep 2007
Functions can only return results to cells in which they are entered.

They cannot format or clear cells.

Perhaps event code would serve you better.

See David McRitchie's site for event code.

http://www.mvps.org/dmcritchie/excel/event.htm

Or Chip Pearson's site for similar info.

http://www.cpearson.com/excel/Events.aspx


Gord Dibben MS Excel MVP


On 30 Sep 2007 14:36:22 -0700, plh <(E-Mail Removed)> wrote:

>Hello Everyone,
>I thought this would be easy, but I must have something fundamentally wrong.
>The following function has various cell-clearing test code lines in it, but none
>have any effect on the contents of the cell. I have tried many other ways to do
>it besides what is below.
>
>Function ClearIt(v As Double) As Boolean
> ActiveWorkbook.ActiveSheet.Range("MyData").Select
> Selection.ClearContents
> ActiveWorkbook.ActiveSheet.Range("MyData").ClearContents
> 'Range("C5").Value = ""
> ActiveWorkbook.ActiveSheet.Cells(5, 3).ClearContents
>End Function
>
>Changes to C5 fire the function as D5 contains =ClearIt(C5). My ultimate goal
>is to have a change to any of the cells in the range "MyData" fire "ClearIt"
>which will decide whether or not to clear the cells based on a set of logical
>rules. But I am stuck on what I thought was a very simple test! Can anyone help
>me?
>Thanx,
>-plh


 
Reply With Quote
 
Bernd P
Guest
Posts: n/a
 
      30th Sep 2007
Hello,

you cannot change cell contents with a function.

Take a sub instead.

Regards,
Bernd

 
Reply With Quote
 
Bill Renaud
Guest
Posts: n/a
 
      30th Sep 2007
You cannot change any cells outside of the cell that the function was
called from, if the function was called from a worksheet cell.

You CAN change other cells inside of a function only if the function is
called from a VBA macro.

You can put the line...

Application.Caller

....inside a VBA routine to return information about how Visual Basic was
called (see "Caller Property" in Microsoft Excel Visual Basic Reference).

--
Regards,
Bill Renaud



 
Reply With Quote
 
plh
Guest
Posts: n/a
 
      1st Oct 2007
The information in these links did the trick, thank you!
-plh
PS,
Does everyone top post in these groups? I don't mind doing as the Romans do, but
it separates my sig line from my message.

In article <(E-Mail Removed)>, Gord Dibben says...
>
>Functions can only return results to cells in which they are entered.
>
>They cannot format or clear cells.
>
>Perhaps event code would serve you better.
>
>See David McRitchie's site for event code.
>
>http://www.mvps.org/dmcritchie/excel/event.htm
>
>Or Chip Pearson's site for similar info.
>
>http://www.cpearson.com/excel/Events.aspx
>
>
>Gord Dibben MS Excel MVP
>
>
>On 30 Sep 2007 14:36:22 -0700, plh <(E-Mail Removed)> wrote:
>
>>Hello Everyone,
>>I thought this would be easy, but I must have something fundamentally wrong.
>>The following function has various cell-clearing test code lines in it, but none
>>have any effect on the contents of the cell. I have tried many other ways to do
>>it besides what is below.
>>
>>Function ClearIt(v As Double) As Boolean
>> ActiveWorkbook.ActiveSheet.Range("MyData").Select
>> Selection.ClearContents
>> ActiveWorkbook.ActiveSheet.Range("MyData").ClearContents
>> 'Range("C5").Value = ""
>> ActiveWorkbook.ActiveSheet.Cells(5, 3).ClearContents
>>End Function
>>
>>Changes to C5 fire the function as D5 contains =ClearIt(C5). My ultimate goal
>>is to have a change to any of the cells in the range "MyData" fire "ClearIt"
>>which will decide whether or not to clear the cells based on a set of logical
>>rules. But I am stuck on what I thought was a very simple test! Can anyone help
>>me?
>>Thanx,
>>-plh

>



--
Where are we going and why am I in this HAND BASKET??
 
Reply With Quote
 
Bill Renaud
Guest
Posts: n/a
 
      1st Oct 2007
plh wrote:
<<Does everyone top post in these groups? I don't mind doing as the Romans
do, but
it separates my sig line from my message.>>

Yes. It makes it easier to see your reply without having to scroll to the
bottom of a very long post (most people use newsreaders, not web-based
readers). See Chip Pearson's web site tips for new posters (see rule # 11).
http://www.cpearson.com/excel/newposte.htm
--
Regards,
Bill Renaud



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clearing cells but also not clearing other dependent cells rtlars Microsoft Excel Programming 4 13th Mar 2010 04:46 AM
clearing values of cells in named range(s) so the cells are empty BRC Microsoft Excel Programming 1 10th Jan 2010 06:54 AM
Clearing cells without clearing formulas marsjune68 Microsoft Excel Misc 2 10th Apr 2009 07:39 PM
Filter, clearing visible cells in a range, but leaving hidden cells Jason Chapman Microsoft Excel Discussion 2 27th Jun 2008 02:15 PM
Clearing certain cells anthony Slater Microsoft Excel Misc 2 6th Oct 2004 11:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:03 PM.