+ Reply to Thread
Results 1 to 5 of 5

User Defined Function: multiply array by a constant

  1. #1
    Registered User
    Join Date
    07-24-2008
    Location
    Dallas
    Posts
    17

    User Defined Function: multiply array by a constant

    I am obvioulsy a beginner, but using excel VBA, how do i multiply an array by a constant?

    I have tried many ways... why doesnt this work? I am getting it to return the third element in the array multiplied by 2 just to see if it works...user just selects an array


    Function multiply(a)
    For i = 1 To 4
    a(i) = 2 * a(i)
    Next i
    multiply = a(3)
    End Function

    I would prefer not to do it element by element... any advice?

    Thanks

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi, and welcome to the forum.

    Can you explain what you mean by an array. Are you talking about a VBA array or perhaps an array of cells on the sheet?

    Perhaps if you explain what you're trying to achieve in words, and attach a sample workbook, there may be other ways of doing this.

    Rgds

  3. #3
    Registered User
    Join Date
    07-24-2008
    Location
    Dallas
    Posts
    17
    I am trying to create a function that does something very simple. Whenever you do the "sum" function, you just type =sum( and then highlight an array of numbers) and then the function sums it.

    I am looking to do the same thing where i can create a function and then type =multiply( highlight cells), and the function goes through and multiplies each element in the array by a constant. I have it returning the third element just to see if i am able to create this new array, but this has not been working at all. The problem is actually much more complicated, but if someone could tell me how to do this step, then it would really help.

    Function multiply(a)
    For i = 1 To 4
    a(i) = 2 * a(i)
    Next i
    multiply = a(3)
    End Function


    Thanks- any ideas?

  4. #4
    Forum Contributor
    Join Date
    07-01-2008
    Location
    Cincinnati, OH
    Posts
    150
    Use an array function.

    For example:
    Enter 1 to 9 in cells A1:A9
    Select B1:B9, enter this formula, =A1:A9*C1, confirm array formula with CTRL-SHIFT-ENTER. Should appear as {=A1:A9*C1} in the formula bar.
    Enter your constant is C1.

    See the attached
    Attached Images Attached Images

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi,

    The following is one solution. The function is
    =(InputRange,constant)

    The function will use the first column of an input range.


    Please Login or Register  to view this content.
    HTH

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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