vba. counting skips.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hi.
VBA Code:
Sub AA()
'this code count the interval between the same values
Set rngData = Sheet2.Range("U2:HH2")
For Each cell In rngData
    If cell = 2 Then   '////////////////// ("P2")
        Sheet7.Range("c2").Offset(0, m) = n
        n = 0
        m = m + 1
    Else
        n = n + 1
    End If
Next
End Sub
This code count how many cells the same value jump
The start point to count is “U2”
So the first value to start the count is at Column P2
have the value 2 so the code count from “U2” to the end
all this table change daily (dynamic array).
My little code help me with one line at the time.
I have been trying to figure out how to plug variable that change
In all this changes, but not clue.

I repeat my code already four times in order to illustrate more or less
What I am looking for, and to indicate what parts are changing as well
The size is 36 rows by any number of columns
1638159937688.png

When I rung the first code this is what I got

1638160010223.png

The value on P2 is 2 so if you start to count from U2, the number two is two spaces apart, and after 6 and so on, the same with the rest of the P values.
this four results is because I just copy and paste the same code four time, and change the necesary part for this task.

VBA Code:
Sub AA()
'this code count the interval between the same values
Set rngData = Sheet2.Range("U2:HH2")
For Each cell In rngData
    If cell = 2 Then   '////////////////// ("P2")
        Sheet7.Range("c2").Offset(0, m) = n
        n = 0
        m = m + 1
    Else
        n = n + 1
    End If
Next
End Sub
Sub AB()
Set rngData = Sheet2.Range("U3 : HH3")
For Each cell In rngData
    If cell = 1 Then   '//////////////////("P3")
        Sheet7.Range("c3").Offset(0, m) = n
        n = 0
        m = m + 1
    Else
        n = n + 1
    End If
Next
End Sub
Sub AC()
Set rngData = Sheet2.Range("U4 : HH4")
For Each cell In rngData
    If cell = 2 Then   '//////////////////("P4")
        Sheet7.Range("c4").Offset(0, m) = n
        n = 0
        m = m + 1
    Else
        n = n + 1
    End If
Next
End Sub
Sub AD()
Set rngData = Sheet2.Range("U5 : HH5")
For Each cell In rngData
    If cell = 1 Then   '//////////////////("P5")
        Sheet7.Range("c5").Offset(0, m) = n
        n = 0
        m = m + 1
    Else
        n = n + 1
    End If
Next
End Sub

so what change here:

Set rngData = Sheet2.Range("U2:HH2")
Set rngData = Sheet2.Range("U3 : HH3")
etc.

and also
If cell = 2 Then '////////////////// ("P2") the value is on P2 ...
If cell = 1 Then '//////////////////("P3")
If cell = 2 Then '//////////////////("P4")
and so on
I tried to create a loop for this but do not work, of course, I did wrong.

I couldn't figure out the loop for this or make it shorter.

Thanks for reading this.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Thank you Zot, but I got an error at ►ws7.Range("C" & nRow).Offset(0, m) = n◄
one next missing but that nothing really, thank you.
Opsss...

I did not really test the code. I was just copying your original code since you said it was working fine for one loop. ?

My main objective is to show how you can make dynamic number of column since you mentioned about it being dynamic if I understood it right. Another is how to loop the row. The loop within the loop is purely your code since I don't really understand your requirement. :giggle:

Anyway, glad you got it solved.
 
Upvote 0
Zot I am so glad to see you around here, and thank you very much for your input, and yes was a lesson for me.
 
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,940
Members
449,275
Latest member
jacob_mcbride

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