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