Solved

[VBA / Excel] Score expressions and SUM them

Posted on 2004-09-12
6
266 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks. It helps a lot when the question is as lucid as yours is.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

762 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

10 Experts available now in Live!

Get 1:1 Help Now