+ Reply to Thread
Results 1 to 14 of 14

Userform, Combobox - Sum based on date and value in column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Userform, Combobox - Sum based on date and value in column

    Hi,

    I have dates in column A, drop-down lists with values "P", "R" and "p" in column B, and values in column C.

    I have created Userform with combobox and textbox, in which I would like to add new Sums to existing average of values for month. What I would like is
    to see in Userform also sums for each drop-down list value of selected month.

    Userform example for January:

    Average:....
    P:...
    p:...
    R:...

    I hope you understand, sample is attached !! Userform can be accessed by double clicking in column C.

    P.S. : Also, the existing "Average" calculation is simply reading the cell values with formula. It would be nice If someone told me how to insert this formula in VBA ?!?

    Any help appreciated !
    Attached Files Attached Files

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Try this one.
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Userform, Combobox - Sum based on date and value in column

    A masterpiece, thanks bakerman2, It works great !!

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Glad to help and thanks for the rep.

  5. #5
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Userform, Combobox - Sum based on date and value in column

    Sorry to bother again, but I didn't quite fully test everything. It's not working with numbers over 100.000 (and I need that), I get "overflow" error, bump

    Can you fix this ?
    Last edited by Lukael; 08-18-2015 at 08:47 AM.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Replace top line with this
    Public cP As Double, sP As Double, cR As Double
    In customaverage function replace ladt line with
    CUSTOMAVERAGE = Round(Total / Count, 4)
    If you want other round after decimal change 4 to desired number of items after decimal.

  7. #7
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Userform, Combobox - Sum based on date and value in column

    Ok, thank you. Now It really works. Can I cut a little more time from you to add some explanation of the code ? I can't fully understand it. Thanks for all your help !

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Which part don't you understand because it's rather straight forward coding I used.

  9. #9
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Userform, Combobox - Sum based on date and value in column

    I don't understand how customaverage function works. I need to change It a bit, in order to work. See this sample, this is exacty how columns are placed in my workbook (just one more column to right for values).

    Result of average function in userfom is 131363,1, which is wrong. Result should be 193917, as in cell value in G4.

    What's wrong, range ?
    Attached Files Attached Files

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Because there are empty cells in your value column whe have to take this in account for calculation. (previous example had no empty values)
    See revised code.

    Function CUSTOMAVERAGE(rng As Range, wMonth As Integer)
        cP = 0: sP = 0: cR = 0
        For Each cell In rng
            If Month(cell) = wMonth Then
                Total = Total + cell.Offset(, 3).Value: If cell.Offset(, 3) <> vbNullString Then Count = Count + 1
                Select Case cell.Offset(, 1).Value
                    Case "P"
                        cP = cP + cell.Offset(, 3).Value
                    Case "p"
                        sP = sP + cell.Offset(, 3).Value
                    Case "R"
                        cR = cR + cell.Offset(, 3).Value
                End Select
            End If
        Next cell
        CUSTOMAVERAGE = Round(Total / Count, 1)
    End Function

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Also added error handling when no values are found.
    Function CUSTOMAVERAGE(rng As Range, wMonth As Integer)
        cP = 0: sP = 0: cR = 0
        For Each cell In rng
            If Month(cell) = wMonth Then
                Total = Total + cell.Offset(, 3).Value: If cell.Offset(, 3) <> vbNullString Then Count = Count + 1
                Select Case cell.Offset(, 1).Value
                    Case "P"
                        cP = cP + cell.Offset(, 3).Value
                    Case "p"
                        sP = sP + cell.Offset(, 3).Value
                    Case "R"
                        cR = cR + cell.Offset(, 3).Value
                End Select
            End If
        Next cell
        If Total = 0 Then CUSTOMAVERAGE = 0: Exit Function
        CUSTOMAVERAGE = Round(Total / Count, 1)
    End Function

  12. #12
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Userform, Combobox - Sum based on date and value in column

    Thanks, can you comment this whole function line by line, I don't understand how exactly this works, everything else is reasonable to me ?

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Userform, Combobox - Sum based on date and value in column

    Function CUSTOMAVERAGE(rng As Range, wMonth As Integer)
        cP = 0: sP = 0: cR = 0
        For Each cell In rng 'check every filled cell in column A
            If Month(cell) = wMonth Then 'check if month equals combobox month
                'if the valuecell(column D) isn't empty add value to Total. Increment count for later calculation
                If cell.Offset(, 3) <> vbNullString Then Total = Total + cell.Offset(, 3).Value: Count = Count + 1
                Select Case cell.Offset(, 1).Value 'check dropdown column on each loop for value (column B)
                    Case "P"
                        cP = cP + cell.Offset(, 3).Value
                    Case "p"
                        sP = sP + cell.Offset(, 3).Value
                    Case "R"
                        cR = cR + cell.Offset(, 3).Value
                End Select
            End If
        Next cell
        If Total = 0 Then CUSTOMAVERAGE = 0: Exit Function 'if no values found set result to 0 to avoid error and exit function
        CUSTOMAVERAGE = Round(Total / Count, 1) 'calculation of total values divided by number of values summed.
    End Function

  14. #14
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Userform, Combobox - Sum based on date and value in column

    Thanks a lot bakerman, you're the man !!

+ 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. Userform ComboBox to Select Date
    By charscribbles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-30-2014, 10:27 AM
  2. [SOLVED] UserForm to complete TextBoxes based on ComboBox
    By WimpieOosthuizen in forum Excel General
    Replies: 9
    Last Post: 05-31-2014, 03:43 AM
  3. [SOLVED] Userform textbox value based on combobox selection
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-17-2014, 11:56 AM
  4. Userform Combobox to populate based on selection in another combobox
    By Excelnoub in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-14-2014, 03:34 PM
  5. [SOLVED] VBA Userform ComboBox: Content of ComboBox based off of Conditional Statement
    By Brianandstewie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-16-2013, 03:00 PM
  6. [SOLVED] Excel Userform: Populate other controls (i.e. textbox & combobox) based on combobox select
    By MileHigh_PhD in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 04:50 PM
  7. [SOLVED] Add RowSource to Userform ComboBox based on value on Another ComboBox
    By Baziwan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-10-2012, 01:17 PM

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