How to add a total to end of ListView column

WednesdayC

Board Regular
Joined
Nov 7, 2010
Messages
201
Office Version
  1. 2016
Platform
  1. MacOS
Hi All
Can anyone help me please?

I have a Listview control with 3 columns.

I have written a function that totals Column 2 values.
However, I cannot find how I add this result to the end of all items in Column
2?

i.e. if column 2 data is:-

1
3
10

The end of column 2 would display 14 (ideally, with a blank row before the total entry)

Thank you in advance.
Regards
Wednesday
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I'm not 100% a "Listview control" is a Listbox, but the Listbox is what I'm using below.


This is the specific part toward your inquiry:
Code:
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 0) = "______" '< adjust as desired
NewLB.List(NewLB.ListCount - 1, 1) = "______"
NewLB.List(NewLB.ListCount - 1, 2) = "______"
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 1) = ListSum '< Your Result here

Code:
'Put this bit in a New UserForm to Illustrate
Private Sub UserForm_Initialize()
Set NewLB = Me.Controls.Add("Forms.ListBox.1", "ListBox1", False)
NewLB.Width = 180
NewLB.Move 25, 25
NewLB.Visible = True
NewLB.Clear
NewLB.ColumnCount = 3
NewLB.ColumnWidths = "50 pt,50 pt,50 pt"
NewLB.ColumnHeads = False
NewLB.Move
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 1) = 1
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 1) = 3
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 1) = 10
ListSum = 0
For x = 0 To NewLB.ListCount - 1
    ListSum = ListSum + CLng(NewLB.List(x, 1))
Next x
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 0) = "______"
NewLB.List(NewLB.ListCount - 1, 1) = "______"
NewLB.List(NewLB.ListCount - 1, 2) = "______"
NewLB.AddItem
NewLB.List(NewLB.ListCount - 1, 1) = ListSum
End Sub
 
Upvote 0
You should be able to add it as an item/row in the Listview, though are you sure that's the ideal place for the it?

I know nothing about your data but could it be possible that the total could get mistaken for a value?
 
Upvote 0
Hi Tweedle

Thanks very much for your reply.
Although I am actually using a Listview, rather than a Listbox, your reply has helped me with something else!

A very greatful

Wednesday
 
Upvote 0
Hi Norie

Thanks for your reply.

In my particular circumstances, the total will not be mistaken for a value.
The specific problem I have is how to code for a specific cell in a Listview i.e. I wish my total to appear as the last entry of Column 2.

Can you help?
Regards
Wednesday
 
Upvote 0
I can look something out but I'm a little confused by what you mean by 'cell'.
 
Upvote 0
I believe ultimately it will be something like this:
Code:
With ListView1
 
.ColumnHeaders(2).Alignment = lvwColumnRight
 
calcTotal = 1110.01 'Your Sum
 
.ListItems.Add , , "" 'Blank Row
.ListItems.Add , , "" 'Row to contain Totals
.ListItems(.ListItems.Count).SubItems(1) = calcTotal ' Apply Total
End With
 
Upvote 0
Hi Tweedle

Thank you very much for your reply.
I will have a look at this tomorrow.
Regards
Wednesday
 
Upvote 0
This will add a line at the bottom of the list with a label (TOTALS) in the first column and your total in the second column.

Obviously replace the text 'Your Total' with the result of your calculation and change objListView to the name of your list view control.
 
Upvote 0
Hi Norie

Just to say that I have sorted out my problem now. The total row was being called into the function every time and thus failing.

Thank you for your help in this matter.
Regards
Wednesday
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top