Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Setting Cell Values from VBA

I'm using Excel 2003, and trying to figure out how to set a number of cells
(from VBA) in a worksheet to specific values based on the contents of another
cell. Specifically..

I have one column with the raw data values. The next few columns over, I
need to generate the results of some calculations on each row of the first
column. So far, that's the easy part.. The hard part is, the results can
include anywhere from 1 to 10 values.. So what I'm trying to do is write a
function that returns the first result in the cell with the function
(=myFunc(RC[-1])), but it also needs to write any remaining results to the
next 2 thru 10 columns.

Because the calculations are complex, and there's a couple thousand rows to
process, I'd rather not have to run the same computations repeatedly for each
column just to extract one value from the set.

So far, I've tried something like:

<code
Function myFunc(nData As Integer, thisCell As Range) As Integer
Dim nResults(1 To 10) As Integer
Dim i, j

' (lengthy processing code snipped.. it basically puts the results in
the nResults array, and store the number of results generated in j)

myFunc = nResults(1)
For i = 2 To j
If nResults(i) -1 Then
thisCell.Offset(0, i).Value = nResults(i)
Else
thisCell.Offset(0, i).Value = "#ERR"
End IF
Nexy i
End Function
</code

When calling, the formula I'm using is "=myFunc(RC[-1],RC)", however, Excel
prints the "#VALUE" error value in any cell where there's more than one
result. What am I doing wrong?

Thanks,
C.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Setting Cell Values from VBA

A user defined function can only change the value in the cell it's called
by.

You can return an array from a function, but you have to select enough
cells, and array-enter the function into the range of cells
(CTRL+SHIFT+ENTER).

Set up your UDF so it generates an array of 10 values, and leave the unused
ones blank. The code for the function should populate the array. Select a 10
column wide row of cells, type the formula, and hold CTRL and SHIFT while
pressing ENTER. If done correctly, Excel surrounds the formula in curly
brackets:

{=MYFUNC(RC[-1])}

If you type the curly brackets, it will not work.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Casteele/ShadowLord" wrote
in message ...
I'm using Excel 2003, and trying to figure out how to set a number of
cells
(from VBA) in a worksheet to specific values based on the contents of
another
cell. Specifically..

I have one column with the raw data values. The next few columns over, I
need to generate the results of some calculations on each row of the first
column. So far, that's the easy part.. The hard part is, the results can
include anywhere from 1 to 10 values.. So what I'm trying to do is write a
function that returns the first result in the cell with the function
(=myFunc(RC[-1])), but it also needs to write any remaining results to the
next 2 thru 10 columns.

Because the calculations are complex, and there's a couple thousand rows
to
process, I'd rather not have to run the same computations repeatedly for
each
column just to extract one value from the set.

So far, I've tried something like:

<code
Function myFunc(nData As Integer, thisCell As Range) As Integer
Dim nResults(1 To 10) As Integer
Dim i, j

' (lengthy processing code snipped.. it basically puts the results in
the nResults array, and store the number of results generated in j)

myFunc = nResults(1)
For i = 2 To j
If nResults(i) -1 Then
thisCell.Offset(0, i).Value = nResults(i)
Else
thisCell.Offset(0, i).Value = "#ERR"
End IF
Nexy i
End Function
</code

When calling, the formula I'm using is "=myFunc(RC[-1],RC)", however,
Excel
prints the "#VALUE" error value in any cell where there's more than one
result. What am I doing wrong?

Thanks,
C.



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
Dynamically setting CELL COLORS based on TWO OTHER cell values Tom Excel Discussion (Misc queries) 3 February 22nd 09 07:34 PM
Setting values in a cell Stephen Excel Discussion (Misc queries) 3 July 10th 08 08:23 PM
Need Help Setting Cell Values Jay Excel Programming 1 September 29th 05 04:09 PM
Setting Excel Cell values using C# AmitCh Excel Programming 1 December 30th 04 06:40 AM
Need assist with setting cell values from an addin subroutine [email protected] Excel Programming 1 December 12th 04 01:52 PM


All times are GMT +1. The time now is 04:35 PM.

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

About Us

"It's about Microsoft Excel"