Link to home
Start Free TrialLog in
Avatar of -Polak
-PolakFlag for United States of America

asked on

VBA Summing a Table Inside of a UserForm

Alright I've been at this workbook for 15 hours today and I didn't expect this to fight me... but it is. What's the correct Syntax to Sum and entire table's column in VBA for a field in my userform?

Private Sub drpFHInputMethod_Change()
    If drpFHInputMethod.Value = "From Planning Dashboard" Then
        txtReqFH.Value = Application.WorksheetFunction.Sum(TableFHAnalysisEngine[ApprovedHours])
        txtReqFH.Locked = True
        OptionButtonYes.SetFocus
    Else: txtReqFH.Locked = False
        txtReqFH.Value = ""
        txtReqFH.SetFocus
    End If
End Sub

Open in new window

Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

The easiest way would be to make use of the Table's Total Row assuming you are referring to a Data table. This is untested but attach an example workbook and I'll lcheck it for you

 Dim oTbl As ListObject
    Dim TotalsOn As Boolean
    Set oTbl = ActiveSheet.ListObjects("TableFHAnalysisEngine")
    With oTbl.ShowTotals
        TotalsOn = .ShowTotals
        If .ShowTotals = False Then .ShowTotals = True
        .ListColumns("ApprovedHours").TotalsCalculation = _
        xlTotalsCalculationSum
        MsgBox .Range("TableFHAnalysisEngine[[#Totals],[ApprovedHours]").Value
        .ShowTotals = TotalsOn
    End With

Open in new window

Avatar of -Polak

ASKER

That threw aUser generated image for me. Here's a dummy workbook. Honestly if the best/easiest way is to just have a backendsheet do the worksheet calculation and then pass that value into the  User form I'm not opposed(if you look at my code a bunch of the other buttons already use a backend sheet).... I just thought it would be simple to do this all in VBA.
VBAUSerFormSumming.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of -Polak

ASKER

(looks like I forgot to rename) Nice! I just learned about DatabodyRange and xlCellTypeVisible yesterday. Infact, that's how "Table 1" gets its data in the first place in the real workbook.
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015