Using Loops to Run Through Groups of Data

canada01

New Member
Joined
Mar 20, 2017
Messages
10
Hello,

I am new to VBA and am learning as I go for a work project.
I need to cycle through groups of data (anywhere from 1-319 lines long in a file with 155,000+ lines of data) and perform some operations on them.

I am looking for recommendations on how to create a loop (looking at For/Each/Next loops right now) that will look at the information in column A (data-acq. session ID) to define each session as a "group" of data, so that the actions thereafter are only performed on that group, and not on the entire sheet. An example of the data is screenshot-ed below.

Thanks in advance for any advice!

[TABLE="width: 500"]
<tbody>[TR]
[TD]Session ID[/TD]
[TD]VTM Index[/TD]
[TD]Parameter Name[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]PD143148
[/TD]
[TD]0[/TD]
[TD]Position[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]1[/TD]
[TD]Speed[/TD]
[TD]34[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]2[/TD]
[TD]Odometer[/TD]
[TD]9110[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]3[/TD]
[TD]Position[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]4[/TD]
[TD]Odometer[/TD]
[TD]9156[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]5[/TD]
[TD]Speed[/TD]
[TD]56[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]6[/TD]
[TD]Speed[/TD]
[TD]72[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]7[/TD]
[TD]Position[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]8[/TD]
[TD]Odometer[/TD]
[TD]9234[/TD]
[/TR]
[TR]
[TD]PD143148[/TD]
[TD]9[/TD]
[TD]Position[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][TABLE="width: 167"]
<tbody>[TR]
[TD="class: xl65, width: 167"]PD129834[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]0[/TD]
[TD]Odometer[/TD]
[TD]324[/TD]
[/TR]
[TR]
[TD][TABLE="width: 167"]
<tbody>[TR]
[TD="class: xl65, width: 167"]PD129834[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]1[/TD]
[TD]Odometer[/TD]
[TD]329[/TD]
[/TR]
[TR]
[TD][TABLE="width: 167"]
<tbody>[TR]
[TD="class: xl65, width: 167"]PD129834[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]2[/TD]
[TD]Position[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][TABLE="width: 167"]
<tbody>[TR]
[TD="class: xl65, width: 167"]PD129834[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]3[/TD]
[TD]Speed[/TD]
[TD]100[/TD]
[/TR]
</tbody>[/TABLE]
 
Session ID is Column "A", there are time stamps and other data that I don't want to get rid of in the columns B-H

In relation to my first code , Can I assume that the highlighting criteria is related to Individual set of unique "Session ID's", in which case The last "Session ID" (PD 129834) would not be highlighted anyway because its a different "ID" and has only one cell with the word "Position". Please confirm !!!

That's correct. The code currently works like how I showed in the example. It looks at the WHOLE sheet. I would like for it to do what you describe above--look at "Position" ONLY in the current Session ID. My trouble is that when I copy the code below (which works on the whole sheet) into the loop your created, Excel crashes with a run-time error. I do not know which run-time error, because the program closes before I can read the numbers.

With Worksheets(1).Range("J1", ActiveCell.SpecialCells(xlLastCell))
Set PSO = .Find("Passenger Seat Occupancy", LookIn:=xlValues)
If Not PSO Is Nothing Then
firstAddress = PSO.Address
Do
Set ValuePSO = PSO.Offset(, 1)
Set PreviousPSO = .FindPrevious(PSO)
Set PrevValuePSO = PreviousPSO.Offset(, 1)
If Not PrevValuePSO Is Nothing And ValuePSO <> PrevValuePSO Then
ValuePSO.EntireRow.Interior.ColorIndex = 33
End If
Set PSO = .FindNext(PSO)
Loop While Not PSO Is Nothing And PSO.Address <> firstAddress
End If
End With
 
Last edited:
Upvote 0
Try this:-
I have written the code so "Session ID" is in column "A" and "Parameter name " is in column "J", I have also included the criteria as "Passenger Seat Occupancy"

Code:
[COLOR="Navy"]Sub[/COLOR] MG23Mar57
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] k [COLOR="Navy"]As[/COLOR] Variant, p [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] nVal [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    [COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] .keys
     nVal = ""
     [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] .Item(k)
         [COLOR="Navy"]If[/COLOR] p.Offset(, 9).Value = "Passenger Seat Occupancy" [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] nVal = "" [COLOR="Navy"]Then[/COLOR]
                nVal = p.Offset(, 10).Value
            [COLOR="Navy"]Else[/COLOR]
               [COLOR="Navy"]If[/COLOR] Not Val(nVal) = p.Offset(, 10).Value [COLOR="Navy"]Then[/COLOR]
                  p.EntireRow.Interior.Color = vbYellow
                  nVal = p.Offset(, 10).Value
               [COLOR="Navy"]End[/COLOR] If
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] p
[COLOR="Navy"]Next[/COLOR] k
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks, Mick! I see how you used the offset command rather than column names to identify the cells with the parameter values.

Could you please explain what the purpose of introducing nVal is? Is this just a name you used in place of "ValuePSO"? I ask because I'll have to recreate this several times to identify other pieces of data, and I would like to be able to give them unique names so that future users will know what is being referenced.
 
Upvote 0
When the code find the value "Passenger Seat Occupancy", then nval is given the value in the next column.
Initially it has no value, but on finding the first "Passenger Seat Occupancy", it is given the value in the next cell.
When the code loops again to find the next "Passenger Seat Occupancy" the value in the next column is checked against it.
If different the line is highlighted and the variable nval is given the new value, so that it can be used for checking the next "Passenger Seat Occupancy" offset value .
When a new "Session ID" (next K value) appears nVal is set to empty and the process start again.
 
Upvote 0
Hi MickG,

Thanks! I ended up stepping through the code in Debug and watched what it was doing.

Thanks again for all your help.
 
Upvote 0

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