+ Reply to Thread
Results 1 to 5 of 5

Looping excel function using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    04-04-2014
    Location
    Warsaw
    MS-Off Ver
    Excel 2010
    Posts
    97

    Looping excel function using VBA

    Hello,
    Can anyone help me with this function that I want to loop:
    =IF(A1/A8<=1;A1/A8;IF(A1/(SUM(A8:B8))<=1;1+A1/(SUM(A8:B8));IF(A1/(SUM(A8:C8))<=1;2+A1/(SUM(A8:C8));IF(A1/(SUM(A8:D8))<=1;3+A1/(SUM(A8:D8));"a lot"))))
    Here;s exapmle:
    examplee.xlsx

    Instead of "a lot" it should expand this formula up to reaching (24 + 0,xx) value, and it should work in different cells.
    So I was thinking about something like creating
    =NEWFORMULA(A1;A8)
    and then in VBA
    Function NEWFORMULA(A As Range, B As Range)
    and that's it.. I don't have idea how to go on with it, I think OFFSET could be useful but I don't have any specific idea how to use it. Maybe someone can give me a help and show me direction.

    Thanks in advance,
    Regards

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Looping excel function using VBA

    in VBA you have ifs to,
    so you'll need to follow the syntax

    if condition then

    else <-- optional else

    end if

    so......


    public function NEWFORMULA(rng1 as excel.range,rng2 as excel.range)

    dim intRng1Value as integer
    dim intRng2Value as integer

    if intRng1Value>intRng2Value then
    IF(range("a1")/(SUM(rng1.address & ":" & rng2.address)<=1

    .......and so on
    end function
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    04-04-2014
    Location
    Warsaw
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Looping excel function using VBA

    Ok, but it gives me Value in the cell on the right from B, and I want to give me SUM of them B.Value + B.Offset(0,1).Value etc. if you know what I mean.

  4. #4
    Registered User
    Join Date
    04-04-2014
    Location
    Warsaw
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Looping excel function using VBA

    Thanks nathansav, but I want it to be short, so it can be counted quickly if I'll put few thousands of this functions. Here's something what I came up with, but it has circular reference:
    Function NEWFUNCTION(A As Range, B As Range)
    
    On Error Resume Next
    
    SUMA = A.Value
    
    For i = 0 To 24
    Range(B).Offset(0, i).Select
    SUMB = Selection.Value
    
    If SUMA / SUMB <= 1 Then
        
        NEWFUNCTION = i + (SUMA / SUMB)
        Exit Function
    Else
        NEWFUNCTION = 0
    End If
    Next i
    
    End Function

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Looping excel function using VBA

    it will just be B.offset(0,i), so SUMB=B.offset(0,i).value
    Last edited by nathansav; 05-29-2014 at 10:50 AM.

+ 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. [SOLVED] Looping a function
    By bharbir in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2013, 09:53 PM
  2. [SOLVED] looping replace function
    By igoodable in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-08-2013, 05:26 PM
  3. [SOLVED] Find All function looping in Excel 2010
    By XML2005 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-12-2013, 12:02 AM
  4. Looping function in Excel
    By Mahabaskar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2013, 10:25 AM
  5. [SOLVED] Need help with looping function
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2006, 06:15 AM

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