Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default assign value to range in VBA function

This code works in a VBA Sub but not in a VBA function
Dim wsPurchase As Worksheet
Set wsPurchase = Worksheets("Purchase")
Dim rCriteria As Range
Set rCriteria = wsPurchase.Range("A1:A2")
rCriteria.Offset(1, 0).Value = sSet

Is there a way to change a worksheet cell from within a VBA function?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default assign value to range in VBA function

No. A function can only return a value (replace its call). You cannot change
anything in a worksheet or Excel's settings from a function.

--
Kind regards,

Niek Otten

"excelman" wrote in message
...
This code works in a VBA Sub but not in a VBA function
Dim wsPurchase As Worksheet
Set wsPurchase = Worksheets("Purchase")
Dim rCriteria As Range
Set rCriteria = wsPurchase.Range("A1:A2")
rCriteria.Offset(1, 0).Value = sSet

Is there a way to change a worksheet cell from within a VBA function?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default assign value to range in VBA function

Not knowing what sSet was and assuming s meant string I put quotes around
sSet.

The code worked fine from both a Subroutine and Function, as was expected.
I'm using Excel 2002 SP3 under Win XP SP1.

--
My handle should tell you enough about me. I am not an MVP, expert, guru,
etc. but I do like to help.


"excelman" wrote in message
...
This code works in a VBA Sub but not in a VBA function
Dim wsPurchase As Worksheet
Set wsPurchase = Worksheets("Purchase")
Dim rCriteria As Range
Set rCriteria = wsPurchase.Range("A1:A2")
rCriteria.Offset(1, 0).Value = sSet

Is there a way to change a worksheet cell from within a VBA function?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default assign value to range in VBA function

Yes and your code should work the same way.... except if you are calling the
function from a cell ie you are using the function as a worksheet function
(in C4: =MyFunc(...) ) in which case changing the sheet/cells through the
function is not allowed.
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"excelman" wrote:

This code works in a VBA Sub but not in a VBA function
Dim wsPurchase As Worksheet
Set wsPurchase = Worksheets("Purchase")
Dim rCriteria As Range
Set rCriteria = wsPurchase.Range("A1:A2")
rCriteria.Offset(1, 0).Value = sSet

Is there a way to change a worksheet cell from within a VBA function?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default assign value to range in VBA function


A function can only change the value in the cell from which it i
called.

There are several answers to what you want to do but a bit mor
information on exactly what would help

--
tony
-----------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...fo&userid=2107
View this thread: http://www.excelforum.com/showthread.php?threadid=51029



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default assign value to range in VBA function

< except if you are calling the function from a cell

A very useful addition! I tend to forget that and always assume functions
are called from a worksheet.

Thanks!

--
Kind regards,

Niek Otten

"sebastienm" wrote in message
...
Yes and your code should work the same way.... except if you are calling
the
function from a cell ie you are using the function as a worksheet function
(in C4: =MyFunc(...) ) in which case changing the sheet/cells through the
function is not allowed.
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"excelman" wrote:

This code works in a VBA Sub but not in a VBA function
Dim wsPurchase As Worksheet
Set wsPurchase = Worksheets("Purchase")
Dim rCriteria As Range
Set rCriteria = wsPurchase.Range("A1:A2")
rCriteria.Offset(1, 0).Value = sSet

Is there a way to change a worksheet cell from within a VBA function?



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default assign value to range in VBA function

I am using the function as a worksheet function C4: =MyFunc(...)
I am trying to set the criteria dynamically each time the MyFunc(sSet) is
called
total = Application.WorksheetFunction.DSum(rDB, rColumn, rCriteria)

"tony h" wrote:


A function can only change the value in the cell from which it is
called.

There are several answers to what you want to do but a bit more
information on exactly what would help.


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=510293


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I assign range to variant and use Mike H[_2_] Excel Discussion (Misc queries) 7 June 7th 07 01:40 AM
assign a value to a range travelersway Excel Discussion (Misc queries) 7 November 2nd 05 03:16 PM
Assign Range - Inactive Worksheet jazzjava Excel Programming 6 June 16th 05 01:39 AM
How to assign a variable in a range select Paul Excel Programming 5 June 3rd 05 11:50 PM
Assign a random selection to a range Tracy D. Excel Programming 3 January 14th 05 07:31 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"