add message & disable the code based on condition

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
471
Office Version
  1. 2019
hi

I have this code splits column into multiple columns , but if the spaces among the items are differnt I mean it's not one space among the items it will show error subscript out of range .

so what I want if the space is not equal one space among the items should show message " there are differnt spaces should correct them " and highlighted the cells by red into column D which doesn't contain one space among the items and disabling the macro and doesn't split the column until correct the space for the items are exsted into cells for column D .
note: the macro wll split column into four columns based on containing the cells in column D contains four items.
VBA Code:
Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim tmpArray() As String

    '~~> This is the relevant sheet
    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

Bridgestone SPLIT).xlsm
D
1MRGED
2BS 1200R20 TCF G580 JAP
3BS 1200R20 TC G580 THI
4BS 1200R24 G582 JAP
5BS 13R22.5 R187 JAP
6BS 1400R20 T VSJ JAP
7BS 155R12C R623 IND
8BS 165R13C R624 IND
9BS 175/70R13 EP150 IND
10BS 175/70R14 EP150 THI
11BS 1800R25 VKT** JAP
12BS 185/65R14 EP150 IND
13BS 185/65R14 TEC THI
14BS 185/65R15 T005 IND
15BS 185/65R15 T01 JAP
16BS 185/70R13 EP150 IND
17BS 195/60R15 EP150 THA
18BS 195/65R15 EP15 JAP
19BS 195/65R15 EP150 THI
20BS 195/70R15C R623 JAP
21BS 195R14C 613 JAP
22BS 195R15C R623 THI
23BS 205/60R16 T001 JAP
24BS 205/65R15 EP150 IND
25BS 205/70R15C R623 THI
26BS 205R16C D840 THI
27BS 215/50R17 EP300 THI
28BS 215/55R16 EP300 IND
29BS 215/55R17 AR20 IND
30BS 215/55R17 GR90 IND
31BS 215/55R17 MY-02 THI
32BS 215/60R16 EP300 THI
33BS 215/60R16 T005A THI
34BS 215/70R15C R623 THI
35BS 225/55R16 EP300 IND
36BS 225/60R16 EP300 IND
37BS 225/60R16 T001 JAP
38BS 225/70R15C R623 JAP
39BS 225/70R16 D687 JAP
40BS 225/70R16 H005 THI
41BS 225/70R17 D697 THI
42BS 235/55R17 T005 THA
43BS 235/55R17 ER300 JAP
44BS 235/55R18 E031 JAP
45BS 235/60R16 T01 JAP
46BS 245/40ZR20 S001 JAP
47BS 245/45R17 T05A JAP
48BS 245/45R18 EP300 THI
49BS 245/45R19 T005 JAP
50BS 245/70R17 684A JAP
51BS 245/75R17 693A JAP
52BS 255/70R15C D840 THI
53BS 265/50R20 DHPA JAP
54BS 265/60R18 D840 JAP
55BS 265/65R17 D840 JAP
56BS 265/70R16 D840 THI
57BS 265/70R18 D684 JAP
58BS 275/40R18 S007 JAP
59BS 275/40R20 Dsport JAP
60BS 275/45R19 AL01 JAP
61BS 275/55R17 Dsport JAP
62BS 275/55R20 AL01 JAP
63BS 275/65R18 AL01 JAP
64BS 275/70R16 D694 JAP
65BS 285/60R18 DHPS JAP
66BS 285/65R17 R683 JAP
67BS 315/80R22.5 R152 JAP
68BS 315/80R22.5 R184 JAP
69BS 315/80R22.5 R184 THI
70BS 315/80R22.5-18PR G580 JAP
71BS 315/80R22.5-18PR G582 THI
72BS 325/95R24 G582 JAP
73BS 385/65R22.5 R164 JAP
74BS 385/65R22.5 R164 THA
75BS 425/65R22.5 R164 JAP
76BS 445/65R22.5 R164 JAP
77BS 650R16 R230 JAP
78BS 700R16 R230 JAP
79BS 750R16 R230 JAP
80BS 750R16 VSJ JAP
81BS LT285/75R16 AT001 JAP
82DT 315/80R22.5 T DS50 THI
83DT 385/65R22.5 DT40 THI
84FS 205/65R15 TZ700 JAP
85FS 215/55R17 TZ700 JAP
ITEM

any suggestion expert to complete this code ?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Will there always be only 4?
 
Last edited:
Upvote 0
Try:
VBA Code:
Sub Sample()
    Dim ws As Worksheet, tmpArray() As String, LastRow As Long, i As Long, RowCount As Long
    Set ws = ThisWorkbook.Sheets("Sheet1")
    With ws
        LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
        For i = 2 To LastRow
            tmpArray = Split(.Range("D" & i).Value, " ")
            If UBound(tmpArray) > 3 Then
                Range("D" & i).Interior.ColorIndex = 3
            End If
        Next i
        .Range("D1").CurrentRegion.AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
        RowCount = .[subtotal(103,D:D)] - 1
        .Range("D1").AutoFilter
        If RowCount > 1 Then
            MsgBox ("Please correct the different spaces in the colored cells.")
            Exit Sub
        Else
            For i = 2 To LastRow
                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)
            Next i
        End If
    End With
End Sub
 
Upvote 0
Solution
If there is always only four, you could use the Trim function to remove any extra spaces to correct extra spaces first - then check to ensure there are four elements.

VBA Code:
Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim tmpArray() As String
    Dim BadFormat As Integer
    '~~> This is the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
BadFormat = 0
With ws
        lastRow = .Range("D" & .Rows.Count).End(xlUp).Row
        For i = 2 To lastRow
            'If InStr(1, .Range("D" & i).Value, " ") Then ' I remarked this, as I was not sure what the purpose was
            .Range("D" & i).Value = WorksheetFunction.Trim(Range("D" & i)) ' this will take out extra spaces
                tmparray = Split(.Range("D" & i).Value, " ")
                If UBound(tmparray) - LBound(tmparray) = 3 Then 'there must be four elements in the array
                  .Range("E" & i).Value = tmparray(0)
                  .Range("F" & i).Value = tmparray(1)
                  .Range("G" & i).Value = tmparray(2)
                  .Range("H" & i).Value = tmparray(3)
                  .Range("D" & i).Interior.ColorIndex = 0
                Else
                    .Range("D" & i).Interior.Color = vbRed
                    BadFormat = 1
                End If
            'End If ' this was from the IF statement I commented out above.
        Next i
End With
If BadFormat = 1 Then
    MsgBox "See highlighted rows which are not properly spaced."
End If
End Sub
 
Upvote 0
Something to consider if those are genuine spaces

VBA Code:
With Sheets("Sheet1")
    lr = .Range("D" & .Rows.Count).End(xlUp).Row
    arr = .Range("D2:D" & lr)
    For i = LBound(arr) To UBound(arr)
        arr1 = Split(arr(i, 1), " ")
        counter = 0
        For j = LBound(arr1) To UBound(arr1)
            If Len(arr1(j)) > 0 Then
                .Cells(i + 1, counter + 5) = arr1(j)
                counter = counter + 1
            End If
        Next
    Next
End With
 
Upvote 0
How many items does the cell D & i have?
 
Upvote 0
@RobVos thanks , but it just show the message without highlight any cell and when I correct the space, then the message still show . it shouldn't if the whole cells in column D contain one space among the items and should work the macro by split into four columns ..
 
Upvote 0
@mumps sorry ! this is my mistake . some cells shows five I corrected , but it just highlight cells without shows the message .
 
Upvote 0
The code I posted will take out any extra spaces (auto-correct) in D and then split it into the next four columns. If it is unable to do that because there are not only 4 elements, it does not split D and it should turn the applicable cell in D red. Correct the red cells and run again.

It will only display the message if it found a cell that would not split into four elements.
 
Upvote 0

Forum statistics

Threads
1,216,489
Messages
6,130,959
Members
449,608
Latest member
jacobmudombe

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