Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default sloppy code help

test = ownershp(midd, site)
Range("a" & i).Value = team
Range("b" & i).Value = owner

so i built a UDF ownershp() that returns two different variables (team and owner) based on two other variables (parent site, child midd). currently, i have to use the test = line to run the function. variables team and owner are public so that my code can use them in the main sub as well as the udf. how can i clean it up to not need to use the test = line? test does nothing else other than allow me to call the udf and it bugs me... there has to be a better way
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default sloppy code help

test = ownershp(midd, site)
Range("a" & i).Value = team
Range("b" & i).Value = owner

so i built a UDF ownershp() that returns two different variables
(team and owner) based on two other variables (parent site, child
midd). currently, i have to use the test = line to run the function.
variables team and owner are public so that my code can use them in
the main sub as well as the udf. how can i clean it up to not need to
use the test = line? test does nothing else other than allow me to
call the udf and it bugs me... there has to be a better way


A UDF is a custom cell function that returns a result to the cell using
it! Nor can you use a UDF to affect change in other cells, only the
cell using it in a formula.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default sloppy code help

On Wednesday, December 3, 2014 8:17:26 PM UTC-7, GS wrote:
test = ownershp(midd, site)
Range("a" & i).Value = team
Range("b" & i).Value = owner

so i built a UDF ownershp() that returns two different variables
(team and owner) based on two other variables (parent site, child
midd). currently, i have to use the test = line to run the function.
variables team and owner are public so that my code can use them in
the main sub as well as the udf. how can i clean it up to not need to
use the test = line? test does nothing else other than allow me to
call the udf and it bugs me... there has to be a better way


A UDF is a custom cell function that returns a result to the cell using
it! Nor can you use a UDF to affect change in other cells, only the
cell using it in a formula.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


you can use a user defined function in vba code as well and call it like you would call worksheet functions. but your statement does kind of clarify something for me - UDF's will only return one answer based on the code it compiles (returns a result to the cell calling the funciton). i think in my case instead of a UDF i should just call a seperate sub to return my two values.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default sloppy code help

On Wednesday, December 3, 2014 8:17:26 PM UTC-7, GS wrote:
test = ownershp(midd, site)
Range("a" & i).Value = team
Range("b" & i).Value = owner

so i built a UDF ownershp() that returns two different variables
(team and owner) based on two other variables (parent site, child
midd). currently, i have to use the test = line to run the
function. variables team and owner are public so that my code can
use them in the main sub as well as the udf. how can i clean it up
to not need to use the test = line? test does nothing else other
than allow me to call the udf and it bugs me... there has to be a
better way


A UDF is a custom cell function that returns a result to the cell
using it! Nor can you use a UDF to affect change in other cells,
only the cell using it in a formula.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


you can use a user defined function in vba code as well and call it
like you would call worksheet functions. but your statement does kind
of clarify something for me - UDF's will only return one answer based
on the code it compiles (returns a result to the cell calling the
funciton). i think in my case instead of a UDF i should just call a
seperate sub to return my two values.


If you use a sub then just call it directly, but pass the variables as
args...

Sub Get_Ownership(Team$, Owner$)
'process to args
Team = sTeam: Owner = sOwner
End Sub

...and in the caller...

Dim sTeam$, sOwner$ '($ = as String)

Get_Ownership(sTeam, sOwner)
Range("a" & i).Value = sTeam
Range("b" & i).Value = sOwner

OR

Since your target cells are contiguous...

Dim vaOwnerInf(1)

Get_Ownership(vaOwnerInf())
Range("a" & i).Resize(1, 2) = vaOwnerInf

Sub Get_Ownership(vArr())
'process to array
vArr(0) = sTeam: vArr(1) = sOwner
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default sloppy code help

I am still getting used to arrays, and this seems like a good start with a small 1x2 size array. Eventually Id like manipulate a MUCH larger array set (10 column x 250+ rows). Right now I am doing this via loops, but since I'm assigning values to cells in a spreadsheet instead of in memory via array im under the impression that there is a much more efficient method of doing this.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default sloppy code help

I am still getting used to arrays, and this seems like a good start
with a small 1x2 size array. Eventually Id like manipulate a MUCH
larger array set (10 column x 250+ rows). Right now I am doing this
via loops, but since I'm assigning values to cells in a spreadsheet
instead of in memory via array im under the impression that there is
a much more efficient method of doing this.


Yes, direct read/write to ranges can be quite slow when dealing with
numerous cells. Processing in memory using arrays is orders of
magnitude faster. Thus, I default to using arrays unless absolutely
unavoidable!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


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
Creating excel file, adding code to it from code, VBE window stays BlueWolverine Excel Programming 0 November 5th 09 07:55 PM
How can I modify my code to offset the defined range and repeat theprocedure instead of duplicating my code? [email protected] Excel Programming 4 May 29th 09 10:13 PM
Shorten code to apply to all sheets except a few, instead of individually naming them, and later adding to code. Corey Excel Programming 3 December 11th 06 05:14 AM
Protect Sheet with code, but then code will not Paste error. How do i get around this. Please read for explainations.... Corey Excel Programming 4 November 25th 06 04:57 AM
Excel code convert to Access code - Concat & eliminate duplicates italia Excel Programming 1 September 12th 06 12:14 AM


All times are GMT +1. The time now is 08:08 AM.

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"