code to check condition for each row

  • Thread starter Horatio J. Bilge, Jr.
  • Start date
H

Horatio J. Bilge, Jr.

I am trying to write a macro that will check the last cell in each row of a
range against a condition. If the condition is met, then the value of the
first cell in the row is added to a string. The string will be used for a
message box.

The range("A3:p42") has names in the first column, data in the middle
columns, and totals in the last column. I'm trying to use a for-next loop,
but I'm stuck on how to refer to the correct cell. This is what I have tried:

Private Sub cmdErrorCheck_Click()
Dim e As String

For Each .Row In Me.Range("A3:p42")
If [the last cell].Value > 4 Then
e = e & [the first cell].Value
End If
Next .Row

MsgBox "There is an error on these people: " & e

End Sub
 
D

Don Guillett

try this idea

for i=3 to 42
if cells(i,"p")>4 then msgbox "error on "&cells(i,"a")
next i
 
H

Horatio J. Bilge, Jr.

Thank you for the idea - it works great. This is the final code I used:

Private Sub cmdErrorCheck_Click()
Dim m As String
Dim i As Integer

For i = 3 To 42
If Cells(i, "P").Value > 4 Then
m = m & vbLf & Cells(i, "A").Value
ElseIf Cells(i, "O").Value >3 Then
m = m & vbLf & Cells(i, "A").Value
End If
Next i

MsgBox "There is an error these people: " & m
End Sub


Don Guillett said:
try this idea

for i=3 to 42
if cells(i,"p")>4 then msgbox "error on "&cells(i,"a")
next i

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
Horatio J. Bilge said:
I am trying to write a macro that will check the last cell in each row of a
range against a condition. If the condition is met, then the value of the
first cell in the row is added to a string. The string will be used for a
message box.

The range("A3:p42") has names in the first column, data in the middle
columns, and totals in the last column. I'm trying to use a for-next loop,
but I'm stuck on how to refer to the correct cell. This is what I have
tried:

Private Sub cmdErrorCheck_Click()
Dim e As String

For Each .Row In Me.Range("A3:p42")
If [the last cell].Value > 4 Then
e = e & [the first cell].Value
End If
Next .Row

MsgBox "There is an error on these people: " & e

End Sub
 
D

Don Guillett

Glad to help

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
Horatio J. Bilge said:
Thank you for the idea - it works great. This is the final code I used:

Private Sub cmdErrorCheck_Click()
Dim m As String
Dim i As Integer

For i = 3 To 42
If Cells(i, "P").Value > 4 Then
m = m & vbLf & Cells(i, "A").Value
ElseIf Cells(i, "O").Value >3 Then
m = m & vbLf & Cells(i, "A").Value
End If
Next i

MsgBox "There is an error these people: " & m
End Sub


Don Guillett said:
try this idea

for i=3 to 42
if cells(i,"p")>4 then msgbox "error on "&cells(i,"a")
next i

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
in
message news:[email protected]...
I am trying to write a macro that will check the last cell in each row
of a
range against a condition. If the condition is met, then the value of
the
first cell in the row is added to a string. The string will be used for
a
message box.

The range("A3:p42") has names in the first column, data in the middle
columns, and totals in the last column. I'm trying to use a for-next
loop,
but I'm stuck on how to refer to the correct cell. This is what I have
tried:

Private Sub cmdErrorCheck_Click()
Dim e As String

For Each .Row In Me.Range("A3:p42")
If [the last cell].Value > 4 Then
e = e & [the first cell].Value
End If
Next .Row

MsgBox "There is an error on these people: " & e

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top