+ Reply to Thread
Results 1 to 25 of 25

cell declaration with function of vba

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    cell declaration with function of vba

    Hello,

    I have a Problem with the programming the vba function. It looks like that:

    Function Name (Frequency [Cell A1 to A10], Answer [Cell B1 to B10])
    The workflow for the program is available, but I have problems to bring the Cells from A1 to A10, also B1 to B10, to the line with the function. How can I program that?
    At the end I will give the command on a Cell =myFunction and read the needed Cells.
    Regards
    Last edited by rasid; 12-10-2020 at 10:56 AM.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: function with vba

    are you asking about how to put ranges of cells into arguments of functions?

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,975

    Re: function with vba

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however the thread title does not really convey what your request is about. Tell us what you are trying to do, not how you think it should be done.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Hello vba_php.
    Yes, that is exact what I am looking for. How can I build it, that I can afterwards use it in excel when I want to give the cell values as arguments to the vba function section. Please, you have an idea?

  5. #5
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: cell declaration with function of vba

    check this out:

    https://www.google.com/search?q=exce...tion+arguments

    you can put almost anything in arguments. but not everything. it seems as though this IS possible. let us know if you can't get what you need from that link. it's a great learning tool.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,881

    Re: cell declaration with function of vba

    I wrote this tutorial a while ago that covers my strategies for building UDFs -- including some about using arguments in UDFs. See if this helps with your question: https://www.excelforum.com/tips-and-...uild-udfs.html
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    All you should need is something like this.
    Function Name (rngFreq As Range, rngAns As Range)
       ' /// DO STUFF WiTH rngFreq and rngAns ///
    End Function
    Which can be called from a worksheet cell like this.

    =Name(A1:A10, B1:B10)
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Oh, many thanks. I will discover all and if I would have any question, I will come back to you "all"

  9. #9
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Hello Norie,
    short question. For example, I want to compile like this c=LOG10(CELL(A1)/CELL(A2)).......
    How can I do this without a possibility to enter the RANGE object or the value? I must operate the values of the cells as given points. I will show you my problem in a picture. Thanks
    Attached Images Attached Images

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    Where do A1:A10 and B1:B10 come into this?

    Also, I thought you had a 'workflow' for the program?

  11. #11
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    I thought i can transfer the Cell-Values (A1:A11 and B1:B11) on the begining (after RMS). I thought I can use the cells directly from the excel sheet like you build a curve. The work with it will happen between Function and end function. I think this is easy and I can deal with it. But my problem is to give the cell-values (in this example:
    Frequency Answer
    10 1 <-these are two cells in excel
    15 2 <-these are two cells in excel
    20 5 <-these are two cells in excel

    Where can I transfer this Cell-Values (10,15,20,1,2,5 and many else) from excel to the function?

    c=LOG10(2/1)/LOG10(15/10) -> this is only an example. I will work further with the other elements of the program you can see on the picture (Area and so on). This is easy! But how can I transfer/deliver the 10,15,20,1,2,5 to this function section?

    For example: SUM(CELL1:CELLXX)

    Regards

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    Assuming the values are in cells you can transfer them as I demonstrated in post #7.

    You can then refer to the actual values in the code in various ways.

    For example if you wanted to get a sum of the cells passed via rngFreq you could use this.
    
    sumFreq = Application.Sum(rngFreq)
    If you wanted to loop through the values in A1:A10, and B1:B10 to calculate c you could try this.
    For idx = 1 To rngFreq.Rows.Count-1
        c = LOG10(rngName.Cells(1, idx+1)/rngName.Cells(1, idx))/LOG10(rngFreq.Cells(1, idx+1)/rngFreq.Cells(1, idx))
    Next idx
    P.S. I would suggest you use more appropriate names in the code, do you really want the function to be called Name and the 2nd argument to be named rngName?

    You might also want to introduce variables for x1, x2, y1 and y2.

  13. #13
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Thank you!

  14. #14
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Hello Norie,

    I did everything like I thought it is right. Please see the pictures. But I got the error (I think) #WERT
    What does it mean and where I did a mistake? You can see, I wrote for the CELL =RMS(A1:A9,B1:B9)
    Thanks
    Attached Images Attached Images

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    Rather than images can you upload a sample workbook?

  16. #16
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Sorry, what do you mean with sample workbook? I think the major problem is, how to give the result back to the cell.
    Last edited by rasid; 12-11-2020 at 12:26 PM.

  17. #17
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,881

    Re: cell declaration with function of vba

    As I explained in my tutorial (link in post #6), one of the first things I do when building a UDF is add a Stop statement to it (usually very early in the procedure) so I can enter debug mode use VBA's debugging tools to understand what is happening.

    If I follow the code in the picture correctly, your .cells(1,i) structure suggests that you are expecting to pass a horizontal range to each argument, but your picture of the spreadsheet shows vertical ranges. If I were to debug this, I would add the Stop statement as indicated, then follow what is happening in each loop and see what VBA is doing with a vertical range in code that seems to be expecting a horizontal range. My guess is that you will find that you getting "trying to take the Log of 0" or "divide by 0" errors as VBA encounters empty cells (which are treated as 0).

    Of course, fixing that problem, if that proves to be the problem, is about structuring the procedure so it can expects vertical ranges instead of horizontal (if you know you will always pass vertical ranges to it) or so that it can handle either vertical or horizontal ranges (if you want the procedure to have that flexibility). Maybe include some error trapping if someone passes an invalid range to the function.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    I could try and reproduce things by peering at the images to copy the code and values but it would be far easier if you could upload a workbook with sample data and code.

    If you can't do that you could post the code so we can easily copy it.

  19. #19
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Oh, yes....I am sorry.

    Function Rms(rngFreq As Range, rngAnt As Range)
      Dim c As Double
      Dim area As Double
      c = 0
      area = 0
      Dim i As Long
      For i = 1 To rngFreq.Rows.Count - 1
        c = Log(rngAnt.Cells(1, i + 1) / rngAnt.Cells(1, i)) / Log(rngFreq.Cells(1, i + 1) / rngFreq.Cells(1, i))
        If c > 0 Then
          area = rngAnt.Cells(1, i + 1) / (c + 1) * (rngFreq.Cells(1, i + 1) - rngFreq.Cells(1, i) * (rngFreq.Cells(1, i) / rngFreq.Cells(1, i + 1)) ^ c) + area
        ElseIf c < 0 Then
          If c = -1 Then
            area = rngAnt.Cells(1, i) * rngFreq.Cells(1, i) * Log(rngFreq.Cells(1, i + 1) / rngFreq.Cells(1, i)) + area
          Else
            area = rngAnt.Cells(1, i) / (c + 1) * (rngFreq.Cells(1, i + 1) * (rngFreq.Cells(1, i + 1) / rngFreq.Cells(1, i)) ^ c - rngFreq.Cells(1, i)) + area
          End If
        Else
          area = rngAnt.Cells(1, i + 1) * (rngFreq.Cells(1, i + 1) - rngFreq.Cells(1, i)) + area
      End If
      Next i
      Rms = Sqr(area)
    End Function
    5	0.077488745
    21.625	0.077488745
    29.125	0.830968614
    42.5	0.830968614
    61	0.0508
    175	0.0508
    304	0.0025
    365	0.001454756
    2000	0.001454756
    I hope this helps

  20. #20
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    MrShorty is right, the row/column are the wrong way round which is my fault really as I that's how I had them in post #12.

    Here's the code with the row/column the right way round.
    Function RMS(rngFreq As Range, rngAnt As Range)
    Dim c As Double
    Dim area As Double
    Dim i As Long
    
        For i = 1 To rngFreq.Rows.Count - 1
            c = Log(rngAnt.Cells(i + 1, 1) / rngAnt.Cells(i, 1)) / Log(rngFreq.Cells(i + 1, 1) / rngFreq.Cells(i, 1))
            If c > 0 Then
                area = rngAnt.Cells(i + 1, 1) / (c + 1) * (rngFreq.Cells(i + 1, 1) - rngFreq.Cells(i, 1) * (rngFreq.Cells(i, 1) / rngFreq.Cells(i + 1, 1)) ^ c) + area
            ElseIf c < 0 Then
                If c = -1 Then
                    area = rngAnt.Cells(i, 1) * rngFreq.Cells(i, 1) * Log(rngFreq.Cells(i + 1, 1) / rngFreq.Cells(i, 1)) + area
                Else
                    area = rngAnt.Cells(i, 1) / (c + 1) * (rngFreq.Cells(i + 1, 1) * (rngFreq.Cells(i + 1, 1) / rngFreq.Cells(i, 1)) ^ c - rngFreq.Cells(i, 1)) + area
                End If
            Else
                area = rngAnt.Cells(i + 1, 1) * (rngFreq.Cells(i + 1, 1) - rngFreq.Cells(i, 1)) + area
            End If
        Next i
        
        RMS = Sqr(area)
      
    End Function

  21. #21
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Thank you and MrShorty very much. I will try this. Regards

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    rasid

    Did you try the code I posted? (post #20)

  23. #23
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Hello Norie,

    sorry for the delay. Thank you it worked.
    Nice Weekend!
    Last edited by rasid; 12-12-2020 at 03:18 PM.

  24. #24
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: cell declaration with function of vba

    It worked for me, see the attached workbook.
    Attached Files Attached Files

  25. #25
    Registered User
    Join Date
    12-09-2020
    Location
    London
    MS-Off Ver
    2013
    Posts
    12

    Re: cell declaration with function of vba

    Sorry. I made am mistake and edit my post. Please ignore the first
    Thank you!

+ 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. Right function, Left function, Mid function to extract values in column A
    By bjnockle in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 06-14-2020, 03:14 PM
  2. Replies: 0
    Last Post: 07-07-2018, 04:13 AM
  3. COUNTUNIQUE Function in Google Sheets; Excel lacks a direct counterpart to this function?
    By PivotTablePSHomage in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-18-2018, 05:27 AM
  4. Calling function inside function. (aka nested function)
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 05:58 AM
  5. Replies: 1
    Last Post: 06-10-2015, 12:56 PM
  6. Replies: 13
    Last Post: 04-08-2014, 05:46 AM
  7. Replies: 1
    Last Post: 03-21-2012, 11:22 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