+ Reply to Thread
Results 1 to 10 of 10

Passing row numbers as function argument

  1. #1
    Registered User
    Join Date
    05-05-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    7

    Question Passing row numbers as function argument

    Hi,

    I'm quite new on VBA and need help with a problem. I will try to explain without writing a novel.
    I have an excel sheet with thousands of cells filled with data.
    There are groups of data where I would like to do different kinds of calculations.
    I have made a lot of different vba scripts explicity for each group of cells where I am doing some calculations.
    Now, in order to decrease the number of scripts and make my code more maintainable I would like to send row numbers as argument to functions called from different cells. Then I can use less scripts which will be generic.
    I have those as constants. from time to time I will change the values.

    I put an example here (The important thing is not what is calculated but how I can get my start & end constants to my function).
    The ones listed in the example is only a minor part of my constants.

    So, the constants startGrp1 etc need to go as argument startGrp and endGrp in my script.

    Private Const startGrp1 = 50
    Private Const endGrp1 = 137
    Private Const startGrp1b = 143
    Private Const endGrp1b = 230
    Private Const startGrp2 = 237
    Private Const endGrp2 = 258
    Private Const startGrp2b = 237
    Private Const endGrp2b = 265

    Private Const StartCpuCol = 15
    Private Const EndCpuCol = 16
    Private Const CpuInst = 14
    Private Const Load = 22



    Function Cpu_test(cpu As Variant, startGrp As Integer, endGrp As Integer)
    Dim ret As Double
    Dim counter As Integer
    Dim divisor As Integer
    ret = 0
    For counter = startGrp To endGrp
    If Worksheets("mySheet1").Cells(counter, StartCpuCol).Value <= cpu Then
    If Worksheets("mySheet1").Cells(counter, EndCpuCol).Value >= cpu Then
    divisor = Worksheets("mySheet1").Cells(counter, CpuInst).Value
    ret = ret + (Worksheets("mySheet1").Cells(counter, Load).Value) / divisor
    End If
    End If
    Next
    Cpu_test = ret
    End Function


    Hope someone can help :-)
    Kind Regards,
    Ulf

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Passing row numbers as function argument

    i don't understand why you can't use this function like any other UDF?

    For example (not sure what cpu is, I'll just use 1 as a placeholder):

    Please Login or Register  to view this content.
    etc. etc. etc.

    what am I missing?
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Passing row numbers as function argument

    Cervinus, please take a moment to read the Forum Rules, especially #3?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Registered User
    Join Date
    05-05-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    7

    Re: Passing row numbers as function argument

    Hi,

    I understand, sorry about that.

    Cervinus

  5. #5
    Registered User
    Join Date
    05-05-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    7

    Re: Passing row numbers as function argument

    Hi,

    Thanks for quick reply and sorry for being unclear.

    The thing is that I have lots and lots of different VBA scripts for different kinds of calculations.
    The code snippet I showed was only one out of many. Also the constants that I show is only a small part of what I use.
    I run different script from different cells and same scripts from many cells as well.
    I use three different worksheets that work together and the main worksheet contains about 200 000 cells.
    Today my VBA code spans over approximately 30000 lines of code with 2000 functions.

    The idea is to reduce the number of code lines and functions which will be possible if I can send row numbers (my constants) as function arguments.
    Today I have explicit VBA scripts for each group of data (spanning over specific rows).
    Part of the problem is that approximately once a month I are changing the rows for different groups I use and if I use the method you suggest I will have to change manually in 50-100 cells.

    If I can decrease lines of code excel will (hopefully) recalculate faster.

  6. #6
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Passing row numbers as function argument

    Pleaser upload a copy of your workbook, we can then get a better understanding of what you have and need.
    If someone has helped you then please add to their Reputation

  7. #7
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Passing row numbers as function argument

    if however you're simply wanting to pass a row number to a function then its just an integer in your receiving function, to send the row of a cell thats been changed you could do:

    Please Login or Register  to view this content.
    or use it in an On Click event the same way

  8. #8
    Registered User
    Join Date
    05-05-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    7

    Re: Passing row numbers as function argument

    Hi,
    Thanks :-)

    I will try your suggestion. However it will take a while since I'll be out of town for a couple of days.
    Sorry for not being able to test right away since you guys are so quick with your help, but I will come back with result (if positive) or upload a copy of my workbook.

  9. #9
    Registered User
    Join Date
    05-05-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    7

    Re: Passing row numbers as function argument

    Hi,

    My problem is solved :-)

    Instead of using constants I put my row identifiers in cells and pass those cell references as argument to my functions.

    Please Login or Register  to view this content.
    and my function call looks like this now:

    Please Login or Register  to view this content.
    Maybe not the best solution but it works fine for my purpose :-)
    Thanks again for your assistance. I appreciate it :-)

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Passing row numbers as function argument

    Glad your issue got resolved, and thanks for sharing the solution.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help with VBA passing an argument
    By airbus320 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2014, 11:13 AM
  2. [SOLVED] Passing a 2d array as an argument from a function
    By drstrings in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-14-2013, 01:05 PM
  3. [SOLVED] ByRef argument type mismatch / Passing array as a function argument
    By pzling in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 06:23 PM
  4. passing argument in a function as integer problem
    By ina in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2006, 11:55 AM
  5. Passing a Function name as a procedure argument
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-07-2005, 11:00 AM
  6. [SOLVED] Function (array argument, range argument, string argument) vba
    By Witek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-24-2005, 11:07 AM
  7. Passing an array as argument for custom Function
    By in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-07-2005, 01:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1