Macro to goes through each row to find value, if found create a message box

abiggale

New Member
Joined
Jun 25, 2014
Messages
3
Hi everyone,

I have created a macro that should go through each row of a worksheet and look at two columns for specific values. If these values are in a row, a message box should appear asking to confirm that these details are true. If you click yes, the macro will continue, if you press no, the workbook will close without saving. I am getting an error when I run this macro and I am not sure why, can someone help?

Code:
      Dim x As String     
      Dim y As String
      Dim i As Integer
      Dim found As Boolean
      Dim Message As String


      Sheet1.Select
      Range("A1").Select
      x = "CIBC"
      y = "FRN"
      found = False
      i = 1
      Do Until IsEmpty(ActiveCell)
         If Cells(i, 4).Value = x And Cells(i, 6).Value = y Then
            found = True
            If found = True Then
                Message = MsgBox("Please confirm that the CUID for broker: " & Cells(i, 4) & " and product: " & Cells(i, 21) & " is " & Cells(i, 19), vbYesNo, "CIBC FRN")
                If Message = vbNo Then
                    ActiveWorkbook.Saved = True
                    ActiveWorkbook.Close
                    Else
                End If
            End If
            End If
         i = i + 1
      Loop
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
In what line is the error showing?
I ran the code step by step i did everything is was supposed to do...
 
Upvote 0
What about:
(Try the code on a copy of your original file)

Rich (BB code):
 Dim x As String
      Dim y As String
      Dim i As Integer
      Dim found As Boolean
      Dim Message As String




      Sheet1.Select
      Range("A1").Select
      x = "CIBC"
      y = "FRN"
      found = False
      i = 1
      Do Until IsEmpty(ActiveCell)
         If Cells(i, 4).Value = x And Cells(i, 6).Value = y Then
            found = True
            If found = True Then
                Message = MsgBox("Please confirm that the CUID for broker: " & Cells(i, 4) & " and product: " & Cells(i, 21) & " is " & Cells(i, 19), vbYesNo, "CIBC FRN")
                If Message = vbNo Then
                   ThisWorkbook.Save
                   ThisWorkbook.Close
                    Else
                End If

End If
            End If
         i = i + 1
      Loop
End Sub
 
Upvote 0
What about:
(Try the code on a copy of your original file)

Rich (BB code):
 Dim x As String
      Dim y As String
      Dim i As Integer
      Dim found As Boolean
      Dim Message As String




      Sheet1.Select
      Range("A1").Select
      x = "CIBC"
      y = "FRN"
      found = False
      i = 1
      Do Until IsEmpty(ActiveCell)
         If Cells(i, 4).Value = x And Cells(i, 6).Value = y Then
            found = True
            If found = True Then
                Message = MsgBox("Please confirm that the CUID for broker: " & Cells(i, 4) & " and product: " & Cells(i, 21) & " is " & Cells(i, 19), vbYesNo, "CIBC FRN")
                If Message = vbNo Then
                   ThisWorkbook.Save
                   ThisWorkbook.Close
                    Else
                End If

End If
            End If
         i = i + 1
      Loop
End Sub


Hi,

The macro is supposed to close the workbook without saving. If you run the macro step by step it works but at the end it gives me an error that says "overflow" I am not sure what that means so I'm having trouble fixing it. Can you help?
 
Upvote 0
I got it to work, The error was because the loop continued until "i" maxed out. thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

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