add message & disable the code based on condition

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
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 ?
 
I pasted the data you posted into an Excel sheet and ran the macro. It highlighted the appropriate cells in red and displayed the message "Please correct the different spaces in the colored cells." I don't know why the message isn't displaying for you.
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
@mumps again my apologies ! I was implement the code at another sheet is not matched as in the code that's why I have some problems .
your code works very well. many thanks
 
Upvote 0
I pasted your data and ran the code:

Original:
1643479703519.png


After Code runs There are four total remaining cells that need correction (highlighted red) - they have more than four parts when split.
Without the Trim function, there would be ten to correct. I would also add another if statement checking for data in E and skipping code if present.
1643479758735.png


1643479778659.png
 
Upvote 0
@RobVos my apologies ! thank you to guide me how your code should work.
your code is very excellent ! thanks so much
have a nice weekend !
 
Upvote 0
On the second time running, it will run much faster if you add:

If .Range("E" & i) = "" Then
right after the
For i = 2 To lastRow
and put an End If right before the line
Next i
 
Upvote 0
@steve the fish Forgive me!

I have been busy with others on how to solve the problem. Although your code does not do what I want like other members, but I tried it and found it's very flexible as it divides the cell into several cells regardless of the content of the cell . It is really useful code.
thanks so much for your contribution ;)
 
Upvote 0

Forum statistics

Threads
1,224,836
Messages
6,181,248
Members
453,026
Latest member
cknader

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