Solved

[VBA / Excel] Score expressions and SUM them

Posted on 2004-09-12
6
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

738 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