Solved

[VBA / Excel] Score expressions and SUM them

Posted on 2004-09-12
6
267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now