473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Totaling a data grid

13
Hi,

I'm trying to sum each column and each row of a datagrid and put the totals of columns at the bottom of each column and totals of row at the last empty column.

How do I do this?

I found the following at some MS site but it doesn't put the results anywhere that I can see.

Thank you so much in advance!!!!

Linda
Expand|Select|Wrap|Line Numbers
  1. Sub TotalRowsAndColumns()
  2. ' This macro assumes that you have selected any cell or group of
  3. ' cells within a rectangular region of cells that you would like to
  4. ' have totaled. The totals will appear in the row below and the
  5. ' column to the right of the current region.
  6. Dim r As Integer
  7. Dim c As Integer
  8. Dim i As Integer
  9. Dim j As Integer
  10. Dim myArray As Variant
  11. ' Declaring myArray as a Variant prepares it to receive a range of
  12. ' cells. At that point it is transformed automatically into an array
  13. ' with beginning subscript myArray(1,1).
  14. 'Refer to the region surrounding the current selection
  15. With Selection.CurrentRegion
  16. r = .Rows.Count
  17. c = .Columns.Count
  18. 'resize for totals row and column and place into array
  19. myArray = .Resize(r + 1, c + 1)
  20. ' The following 10 lines of macro code (including comments)
  21. ' are what is commonly referred to
  22. ' as a Nested Loop. The variable i keeps track of the row number,
  23. ' while j keeps track of the column number. Every time j cycles
  24. ' through the available columns, i gets incremented by one and j
  25. ' starts the cycle from one to c all over again.
  26. For i = 1 To r
  27.    For j = 1 To c
  28.       'total for row i
  29.       myArray(i, c + 1) = myArray(i, c + 1) + myArray(i, j)
  30.       'total for column j
  31.       myArray(r + 1, j) = myArray(r + 1, j) + myArray(i, j)
  32.       'grand total
  33.       myArray(r + 1, c + 1) = myArray(r + 1, c + 1) + myArray(i, j)
  34.    Next j
  35. Next i
  36. ' Return the array, which now contains an extra row and column
  37. ' for the totals, to the worksheet.
  38. .Resize(r + 1, c + 1) = myArray
  39. End With
  40. End Sub
  41.  
Aug 30 '07 #1
2 1161
Dököll
2,364 Expert 2GB
How about this:

Expand|Select|Wrap|Line Numbers
  1. With MSHFlexGrid1 
  2.       For iCol = 0 To MSHFlexGrid1.Cols - 1 
  3.         If iCol = 1 Then 
  4.           mstTitle = .TextMatrix(iRow, iCol) 
  5.         End If 
  6.         If iCol = 2 Then 
  7.           mstDescription = .TextMatrix(iRow, iCol) 
  8.         End If 
  9.         If iCol = 3 Then 
  10.           mstRecordID = .TextMatrix(iRow, iCol) 
  11.           GetRecordID 
  12.         End If 
  13.         stColumnHeader = MSHFlexGrid1.ColHeaderCaption(0, iCol) & ": " 
  14.         stDisplayText = stDisplayText & stColumnHeader & .TextMatrix(iRow, iCol) & vbCrLf & vbCrLf 
  15.       Next 
  16.     End With  
  17.  
Try that, please write if not helpful, Linda...
Good luck with the project!
Aug 30 '07 #2
sukitmw
13
Hi Dokoll,

What abt the function GetRecordID?

I don't have that....

So sorry but I also don't understand the codes so not sure how to Dim the variables.

Can you plse be more specific? I'm dumber than a newbie....

Thanks much and have a super weekend!!!
linda
Aug 31 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jordan O'Hare | last post by:
Hello Everyone, I am after some help with the following: I have a windows application that contains a list box and two data grids. All three controls are binded to a dataset that contains...
2
by: kk | last post by:
Have 2 problems, any help is appreciated. Tab with Grids -------------- BL - fetching data from DB ( 5 secs - 10 rows) Grid Laod - 20 secs Grid Paint on tab change - 20 secs Problem: The...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
6
by: Hutty | last post by:
I've looked around and have yet to find anything that would answer my question regarding formating a column in a datagrid. My grid looks like this as far as data" AMHQCON|51300.01|-3147 The...
3
by: pmud | last post by:
Hi, I have a web page (asp.net, code:c#). I havean html table with text boxes. Based on the user input , records are displayed in the data grid below it. Now the datagrid has a large no. of...
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
4
by: NormAmst | last post by:
I have a list of CPU processing times and job durations for an entire department at work. There are 3 classifications I am maintaining. CPU time during peak hours , CPU time during non peak hours...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.