Proceed Based on Criteria

Haree

Board Regular
Joined
Sep 22, 2019
Messages
146
Office Version
  1. 2016
I have a vba code which segregates data into multiple sheets (did that with help from Mr.Excel forum) now i have to check the data so my Range ("H2:H100") in sheet named "Data Sheet" will give two Values, either "Success" or "Error". If any cell from H2:H100 gives the result "Error". I want a MsgBox "Kindly Check Errors and Try again" and then exit the macro. If every cell reflects success it can proceed to the second part of splitting.
I tried the following

VBA Code:
Dim Error as Range
Dim Cell as Range
Set Error = Range("H2:H100)
For Each Cell in Range
if cell.value = "Error"Then
MsgBox "Kindly Check Errors"
Else
(The Other Code)
End IF
Next Cell
End Sub

What Happens here is it checks each individual cell and and does the same
if H3 has error it skips H3 and goes to H4
and runs the code for H4 if it is Success

P.S: I am totally zero regarding VBA i just got to understand few from the codes i have seen
Thanks in advance
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This will end the code when an error is found, until all errors are resolved !!
VBA Code:
Sub MM1()
Dim r As Range, cell As Range
Set r = Range("H2:H100")
For Each cell In r
    If cell.Value = "Error" Then
    MsgBox "Kindly Check Errors"
    Exit Sub
End If
Next cell
MsgBox " All cells correct !!"
End Sub
 
Upvote 0
Thank You so much for the time
Sorry If the Question is very lame

Sub MM1()
Dim r As Range, cell As Range
Set r = Range("H2:H100")
For Each cell In r
If cell.Value = "Error" Then
MsgBox "Kindly Check Errors"
Exit Sub

SHOULD MY OTHER CODE COME HERE

End If
Next cell
MsgBox " All cells correct !!"
End Sub
 
Upvote 0
What do you want the other code to do ??

VBA Code:
Sub MM1()
Dim r As Range, cell As Range
Set r = Range("H2:H100")
For Each cell In r
If cell.Value = "Error" Then
MsgBox "Kindly Check Errors"
Exit Sub
End If
SHOULD MY OTHER CODE COME HERE
Next cell
MsgBox " All cells correct !!"
End Sub
 
Upvote 0
I checked it, Its working perfectly alright
Thank You so much
 
Upvote 0
Sorry To Trouble you again. The Error part is working perfectly fine, but if everything is correct the main code doesn't work, i am attaching the main code also

Sub Copy_Rows()
Application.ScreenUpdating = False
Dim R As Range, Cell As Range
Set R = Range("H2:H100")
For Each Cell In R
If Cell.Value = "Error" Then
MsgBox "Kindly Check Errors and Try again"
Exit Sub
Else
'MAIN CODE'
Range("B2:B200").Select
Selection.Replace What:=".", Replacement:="-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Dim Drange As Range
Dim psheet As Worksheet
Set Drange = Range("A2:E200")
For Each psheet In Worksheets
psheet.unprotect Password:="*****"
Next psheet
Sheets("Data Sheet").Activate
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Data Sheet").Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
For i = 2 To Lastrow
Lastrowa = Sheets(Cells(i, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(i, 2).Resize(, 5).Copy Sheets(Cells(i, 1).Value).Rows(Lastrowa)
Next
Drange.ClearContents
For Each psheet In Worksheets
If psheet.Name = "Data Sheet" Then
psheet.unprotect Password:="*****"
Else
psheet.Protect Password:="*****"
End If
Next psheet
MsgBox "Data Updated Successfully"
'MAIN CODE'
End If
Next Cell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
What is it doing / not doing ?
Are there any error messages ?
 
Upvote 0
The code basically transfers rows from the main sheet called data sheet to the other sheets and then gives a msg that data has been updated successfully and then clears the data in data sheet . Now it just gives the code data updates successfully but doesn't post data to other sheets and it's looping I have to force end excel
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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