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:
Yes sir data sheet is the active sheet and that is were we will find the errors or success
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Can you upload a SMALL sample of data so we can test the code ?
Either use the XL2BB button or upload to a hosting site like dropbox or similar, then post the link back here !
 
Upvote 0
Hello Sir,

Sub Copy_Rows()
Application.ScreenUpdating = False
Dim Cell As Range, R 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
End If
Next Cell
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:="STOCK"
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:="STOCK"
Else
psheet.Protect Password:="STOCK"
End If
Next psheet
MsgBox "Data Updated Successfully"
Application.ScreenUpdating = True
End Sub

I had changed the position of the End IF and Next Cell i have highlighted them in the Violet colour sir. It Solved the problem. Rechecked it twice with errors and Success samples as well, Thank You so much for your time sir. Had it not been for your code in which i can exit the sub i would have had to spend loads of time in error checking :)
 
Upvote 0

Forum statistics

Threads
1,214,581
Messages
6,120,372
Members
448,957
Latest member
BatCoder

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