Solved

[VBA / Excel] Score expressions and SUM them

Posted on 2004-09-12
6
268 Views
Last Modified: 2010-05-02
Hello,

I would like my code to do the following:

1 | 00:03 | EXPR A
2 | 00:10 | EXPR C
3 | 00:51 | EXPR B
4 | 00:49 | EXPR A
5 | 00:11 | EXPR C
6 | 01:16 | EXPR C
7 | 00:01 | EXPR A

The code should 'score' when e.g. EXPR A is used and
sum the time, which has a link with EXPR A (on same row)

The result, in this case, should be:

EXPR A: 00:53
EXPR B: 00:51
EXPR C: 01:37

In my application many expressions are used, 100 in total.
In expressions an index ( [integer] ) is included, which
perhaps can be used in the code.

Sample expressions:
[20] Button A21 enabled
[33] Machine running
[67] Machine off

I hope it is possible that a piece of code can sum the period
of time of expressions. I hope somebody knows the answer!

Thank you very much in advance!
0
Comment
Question by:McD-nl
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 12037806
Hi,
Do you need code?
But you can do your calculation using commands in Excel's Menu:

1. Sort the column that contains expression using Data>Sort... command so you will have:

    1    0:03    EXPR A
    4    0:49    EXPR A
    7    0:01    EXPR A
    3    0:51    EXPR B
    2    0:10    EXPR C
    5    0:11    EXPR C
    6    1:16    EXPR C

2. Using Data > Subtotals... command:
    At each change in Expression field, use Sum function and add subtotal to Time field.
3. Now using buttons created in the left of your worksheet you can get the result you want.

Thanks
-FA
0
 

Author Comment

by:McD-nl
ID: 12037841
Hello FarzadA,

Thank you for your help!
I already thought about your solution, but the problem is that
the length of the excel sheet (amount of rows / data) can vary.

And I want this code to be totally automatic, because I am not going
to use the application myself. The persons who will be using this
application only should press the 'start' button and the code should
present the result in a different Excel Sheet / Page.

Any idea?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037843
If you still want the VBA that you requested, this is it.

Sub DoSum()
Dim rng As Range
Set rng = ActiveSheet.UsedRange
Dim strNames() As String
Dim dtTotals() As Date
Dim rw As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim bFound As Boolean
For rw = 1 To rng.Rows.Count
    bFound = False
    For i = 0 To k - 1
        If strNames(i) = rng.Cells(rw, 3) Then
            bFound = True
            Exit For
        End If
    Next i
    If Not bFound Then
        ReDim Preserve strNames(k)
        ReDim Preserve dtTotals(k)
        strNames(k) = rng.Cells(rw, 3)
        k = k + 1
    End If
    dtTotals(i) = CDate(rng.Cells(rw, 2)) + dtTotals(i)
Next rw
For i = 0 To k - 1
    Debug.Print strNames(i), dtTotals(i)
Next i
End Sub


0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 12037866
I see you've now asked for the results in another sheet. This creates a new one.

Sub DoSum()
Dim rng As Range
Set rng = ActiveSheet.UsedRange
Dim strNames() As String
Dim dtTotals() As Date
Dim rw As Integer
Dim i As Integer
Dim k As Integer
Dim bFound As Boolean
Dim wks As Worksheet
For rw = 1 To rng.Rows.Count
    bFound = False
    For i = 0 To k - 1
        If strNames(i) = rng.Cells(rw, 3) Then
            bFound = True
            Exit For
        End If
    Next i
    If Not bFound Then
        ReDim Preserve strNames(k)
        ReDim Preserve dtTotals(k)
        strNames(k) = rng.Cells(rw, 3)
        k = k + 1
    End If
    dtTotals(i) = CDate(rng.Cells(rw, 2)) + dtTotals(i)
Next rw
For i = 0 To k - 1
    Debug.Print strNames(i), dtTotals(i)
Next i
Set wks = ActiveWorkbook.Sheets.Add
For i = 0 To k - 1
    wks.Cells(i + 1, 1) = strNames(i)
    wks.Cells(i + 1, 2) = Format$(dtTotals(i), "HH:nn")
Next i
End Sub


0
 

Author Comment

by:McD-nl
ID: 12037895
Whooooooeeei GrahamSkan!

This is quick! This is good! This is exactly what I need!
So few lines of code, so much functionality!

Thank you very much. You really have earned the 500 points!
I owe you one!

Greetings, and thank you again!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037952
Thanks. It helps a lot when the question is as lucid as yours is.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question