Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

cancel running macro based on condition

0

hello 

I  have  this  code 

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim tmpArray() As String

  
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        LastRow = .Range("D" & .Rows.Count).End(xlUp).Row

        For i = 2 To LastRow
            If InStr(1, .Range("D" & i).Value, " ") Then
                tmpArray = Split(.Range("D" & i).Value, " ")
                .Range("E" & i).Value = tmpArray(0)
                .Range("F" & i).Value = tmpArray(1)
                .Range("G" & i).Value = tmpArray(2)
                .Range("H" & i).Value = tmpArray(3)


            End If
        Next i
    End With
End Sub

this  code  divides  the  column D  into  four  columns E, F,G,H  based on  four  items   for  each  cell  in  column D  but  if  there  items  are five item or  more then  it  will show  error subscript  out  of  range  in this  line

 .Range("H" & i).Value = tmpArray(3)
 

so  what  I  want  when  some  cells don't contain four  items ,  then  show message box "you  can't  split  all  of  items  because  there  are  more  than  four items, please  correct  them"  and  cancel run  the  macro  . it  should  not  split column D   .

if  all  of  the  cells in column D  contain four  items  then should  split column D.

thanks 

Answer
Discuss

Answers

0
Selected Answer

Leopard

I suggest you split cells in D (as above) but write them to an output array (which I've declared as OutArray below) before writing to E:H. That way, you can collect the addresses of "incorrect cells" which don't contain just 4 items (into a string variable I've called Not4). At the end, you cand add a test to check if there are no eroors (and write to the columns) or give a modified message so that the user knows which cells have an error.

Changes are in bold and comments added:

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim tmpArray() As String
    Dim OutArray As Variant, Not4 As String


    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
        ReDim OutArray(2 To LastRow, 1 To 4)
        For i = 2 To LastRow
            If InStr(1, .Range("D" & i).Value, " ") Then
                tmpArray = Split(.Range("D" & i).Value, " ")
                 'check there are four items in this row
                If UBound(tmpArray, 1) = 3 Then
                    OutArray(i, 1) = tmpArray(0)
                    OutArray(i, 2) = tmpArray(1)
                    OutArray(i, 3) = tmpArray(2)
                    OutArray(i, 4) = tmpArray(3)
                Else
                    ' if more or less than 4, capture cell address
                    Not4 = Not4 & "D" & i & " "
                End If

            End If
        Next i
        
        If Not4 = "" Then
            ' write Output to columns
            .Range("E2:H" & LastRow).Value = OutArray
            Else
            ' tell user of errors
            MsgBox "You can't  split  all  of  items  because  there  are  not  four items in cell(s) " & Not4 & Chr(13) & Chr(13) & "Please correct items or number of (space) separators "
        End If

    End With
End Sub

I've changed the items in your question file (so they contain only 4 items)- see attached file but added spaces in the second words in D7 and D13 (for test purposes). Note that if you have four "words" but they have a leading space, a trailing space or two spaces rather than 1 then the code will see MORE that 4 items (though some may be null strings i.e. the nothing between two adjacent spaces).

Hope this makes sense and fixes your problem.

Discuss

Discussion

many  thanks  for  your  help !
actually I  like  your idea  about  this
I've modified your message so that the user knows which cell has an error (there may be more).
but unfortunately  just  works  in  one  cell  and  ignore  the  others  cells contain more  than four items .you  can  test it  based on attached file
leopard (rep: 88) Jan 31, '22 at 7:51 am
Leopard. I see you recently added a file to your question but assume my changes work with that file (since you selected my Answer), right? 
John_Ru (rep: 6152) Jan 31, '22 at 7:52 am
Hi John,
sorry ! my  first  comment  I  forgot greetin you .
based  on  my  question  you  answered  me  , but  based  on  your  idea  doesn't  work   . just  I  would  inform  you to   your  idea  doesn't  work  well  as  you  think .
leopard (rep: 88) Jan 31, '22 at 8:01 am
Leopard.

The code does work- see revised Answer (penultimate paragraph) and file. Your entry in D1 had >4 items and two spaces together. Try the "Split button now and it will work.
John_Ru (rep: 6152) Jan 31, '22 at 8:24 am
the  code  does  work  very  well  . what  I'm  talking  about    if  there  are  many  items  more  than  four  items  then  the  message  box   should  show  cells address  contain wrong  items . for  instance if  I  add  in D2 =  BS 1200R20 18PR G580 JAP and  D4  = BS 1200R20 18PR TCF G580 JAP   then  when  show  the  message  box   for  wrong  items for  each  cell like this   D2,D4  , but  based on  your  code  just  show  D2   with  ignore  the  others .
leopard (rep: 88) Jan 31, '22 at 8:37 am
I've revised my Answer to show all "failed" cells.
John_Ru (rep: 6152) Jan 31, '22 at 8:43 am
all  things  are  great !
much  appreciated  for  your  assistance and  time 
leopard (rep: 88) Feb 1, '22 at 12:58 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login