VBA: time difference in any single rows

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm approaching a work, and I would like some good hints to proceed.
In the image an example (actually I have thousands of rows to elaborate).

https://imgur.com/a/AQwvOR3

In the scheme, for any single code I have elaborated a sort of timeline.

Now, I have to set something like this: the user, for instance, would like to know how many times BETA follows CREATION in the timeline, and the time average beetween the two steps: so, in B15 and C15 he has to write the name of the two steps.
Clikking on CALCULATE, the two requested informations should be reported in D15 and E15.

Could it be a logical way to set the work?
Do you suggest anything else?

Thank's in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
hmmmm maybe try this out on the click of the button

Code:
Dim r As Range
Dim stepA, stepB As String
Dim counter As Integer
Dim timeSum, timeAvg As Long
timeSum = 0
counter = 0
stepA = Range("B15").Text
stepB = Range("C15").Text
For Each r In Range("C2:C11")
    If r.Value = stepA Then
        If r.Offset(, 2).Value = stepB Then
            counter = counter + 1
            timeSum = timeSum + (r.Offset(, 1).Value - r.Offset(, -1).Value)
        End If
    End If
Next
timeAvg = timeSum / counter
Range("D15").Value = counter
Range("E15").Value = timeAvg

not tested ... just kinda winged it lol
 
Upvote 0
hmmmm maybe try this out on the click of the button

Code:
Dim r As Range
Dim stepA, stepB As String
Dim counter As Integer
Dim timeSum, timeAvg As Long
timeSum = 0
counter = 0
stepA = Range("B15").Text
stepB = Range("C15").Text
For Each r In Range("C2:C11")
    If r.Value = stepA Then
        If r.Offset(, 2).Value = stepB Then
            counter = counter + 1
            timeSum = timeSum + (r.Offset(, 1).Value - r.Offset(, -1).Value)
        End If
    End If
Next
timeAvg = timeSum / counter
Range("D15").Value = counter
Range("E15").Value = timeAvg

not tested ... just kinda winged it lol

I thank you because it could inspire me.
But something wrong happens: consider that STEP B could also be in the following cells instead in the column E, for example for the codes 1, 3 and 9 BETA is on the column G, so the correct quantity is 5 and not 2 as your macro says.
 
Last edited:
Upvote 0
ahhh ok i was wondering about that but forgot to ask... try this one now... i simply added that condition

Code:
Dim r As Range
Dim stepA, stepB As String
Dim counter As Integer
Dim timeSum, timeAvg As Long
timeSum = 0
counter = 0
stepA = Range("B15").Text
stepB = Range("C15").Text
For Each r In Range("C2:C11")
    If r.Value = stepA Then
        If r.Offset(, 2).Value = stepB Then
            counter = counter + 1
            timeSum = timeSum + (r.Offset(, 1).Value - r.Offset(, -1).Value)
        End If
        [COLOR=#ff0000]If r.Offset(, 4).Value = stepB Then
            counter = counter + 1
            timeSum = timeSum + (r.Offset(, 3).Value - r.Offset(, -1).Value)
        End If[/COLOR]
    End If
Next
timeAvg = timeSum / counter
Range("D15").Value = counter
Range("E15").Value = timeAvg
 
Upvote 0
ahhh ok i was wondering about that but forgot to ask... try this one now... i simply added that condition

Code:
Dim r As Range
Dim stepA, stepB As String
Dim counter As Integer
Dim timeSum, timeAvg As Long
timeSum = 0
counter = 0
stepA = Range("B15").Text
stepB = Range("C15").Text
For Each r In Range("C2:C11")
    If r.Value = stepA Then
        If r.Offset(, 2).Value = stepB Then
            counter = counter + 1
            timeSum = timeSum + (r.Offset(, 1).Value - r.Offset(, -1).Value)
        End If
        [COLOR=#ff0000]If r.Offset(, 4).Value = stepB Then
            counter = counter + 1
            timeSum = timeSum + (r.Offset(, 3).Value - r.Offset(, -1).Value)
        End If[/COLOR]
    End If
Next
timeAvg = timeSum / counter
Range("D15").Value = counter
Range("E15").Value = timeAvg

Now, following your pattern, I'm going to try considering thousand of rows and dozens of columns, with any possible relation beetween STEP A and STEP B (for example, STEP A on column S and STEP B on column AC).
 
Upvote 0
Awesome, yeah keep building on to it, im sure there are many ways of doing this but with my knowledge this is what i came up with so you can study the code and change it to your liking
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top