VBA help, counters and loops

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Hi everyone.

I have a little issue. I have two sheets. A sheet that contains the names of sites, and the minimum guranteed pressures for that site.

I then have another sheet, with the same sites, but daily pressure values for a month.

I am trying to write a macro that will determine the number of times in a dataset that the actual pressures have dropped below the guranteed pressures.

In both sheets, the site name starts in cell (1,2), with the pressure values below the site name. i.e.


Guranteed Pressure list

Code:
     A      B        C         D
1|NULL | Site A | Site B | Site C |
2|NULL | 50     | 60     | 70     |

The actual pressure list is similar:

Code:
     A      B        C         D
1|Date1| Site A | Site B | Site C |
2|Date2| 50     | 60     | 70     |

So for each sheet, the first line of data is in B2. The site names are exactly the same in both sheets.


Now, here is the macro that I have written (that doesn't work):

Code:
Public Sub CntBreach()
Dim RowIn As Long
Dim LRow As Long
Dim IntCnt As Boolean
Dim Cnt As Long

IntCnt = False
LRow = Sheets("Pressures").Range("A" & Rows.Count).End(xlUp).Row
Cnt = 0

If Not ActiveSheet.Name = "Pressures" Then
Sheets("Pressures").Select
End If
For i = 2 To LRow
RowIn = 1
Do
RowIn = RowIn + 1
If RowIn = 121 Then
IntCnt = True
End If
If Sheets("Pressures").Cells(i, RowIn) < Sheets("AllOTCalc").Cells(2, RowIn) Then
Cnt = Cnt + 1
End If

Loop Until IntCnt = True
 
Next i

Sheets("AllOTCalc").Cells(3, 3).Value = Cnt
 
 

End Sub

I am trying to use "for each i" to iterate through the rows, and a loop with a counter "RowIn" to itterate through the columns. i.e for 120 different sites, the macro should check each site against the reference sheet, add one to the counter if the actual pressure is lower than the guranteed, and then at the end of the row, move down to the next row.

Does anyone have any ideas?

Many thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Never mind all, I saw my mistake. I simply moved IntCnt = False, into the "for i" statement.

Cheers anyway, and sorry to anyone who has spent valuable minutes reading this ;)
 
Upvote 0
This forum always comes up with a solution one way or another! :)
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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