Delete words under 3 leters

Floopindoop

New Member
Joined
Jun 21, 2011
Messages
3
I have a macro that splits a paragraph in A1 and puts each seprate word under the B collumn. I need a macro that can delete every cell with a word under 3 letters. It would also be good if it deleted 'the' and 'and'
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Welcome to the board...

Can you post the code you have, I should be fairly simple to ammend it to include this criteria.
 
Upvote 0
Code:
Sub test()
Dim d As Long, c As Range
For d = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
    Set c = Range("B" & d)
    If Len(c) < 3 Or LCase(c) = "the" Or LCase(c) = "and" Then c.Delete xlShiftUp
Next
End Sub
 
Upvote 0
Here's the code i have right now



Sub SplitByWord()
Dim avs As Variant
Dim sAdr As String
Dim sFrm As String

With ActiveCell
avs = Split(.Value, " ")
If UBound(avs) > 0 Then
With .Offset(0, 1).Resize(UBound(avs) + 1)
.Value = Application.Transpose(avs)
sAdr = .Address
sFrm = "if(right(" & sAdr & ", 1) <> "" "", " & sAdr & " & "" "", " & sAdr & ")"
'Debug.Print sFrm
.Value = Evaluate(sFrm)
End With
End If
End With
End Sub
 
Upvote 0
incorporating the method posted by Hotpepper

Code:
Sub SplitByWord()
Dim avs As Variant
Dim sAdr As String
Dim sFrm As String
Dim i As Long
With ActiveCell
    avs = Split(.Value, " ")
    If UBound(avs) > 0 Then
        With .Offset(0, 1).Resize(UBound(avs) + 1)
            .Value = Application.Transpose(avs)
            For i = .Rows.Count To 1 Step -1
                If Len(.Cells(i, 1)) < 3 _
                Or LCase(.Cells(i, 1)) = "and" _
                Or LCase(.Cells(i, 1)) = "the" _
                Then .Cells(i, 1).Delete shift:=xlUp
            Next i
            sAdr = .Address
            sFrm = "if(right(" & sAdr & ", 1) <> "" "", " & sAdr & " & "" "", " & sAdr & ")"
            'Debug.Print sFrm
            .Value = Evaluate(sFrm)
        End With
    End If
End With
End Sub
 
Upvote 0
Re: Delete words under 3 letters

Try this:
Code:
Sub SplitByWord()
    Dim avs         As Variant
    Dim iR          As Long
    Dim iW          As Long
 
    With ActiveCell
        avs = Split(.Value, " ")
        iW = -1
 
        For iR = 0 To UBound(avs)
            If Len(avs(iR)) > 2 And _
               LCase(avs(iR)) <> "and" And _
               LCase(avs(iR)) <> "the" Then
                iW = iW + 1
                avs(iW) = avs(iR) & " "
            End If
        Next iR
 
        If iW <> -1 Then
            ReDim Preserve avs(0 To iW)
            If iW > 0 Then
                .Offset(, 1).Resize(iW + 1).Value = Application.Transpose(avs)
            End If
        End If
    End With
End Sub
 
Upvote 0
or possibly:
Code:
Sub test()
Dim avs, t As Long, s As String, b As Long
avs = Split(Range("A1"))
For t = LBound(avs) To UBound(avs)
    s = LCase(avs(t))
    If Len(s) > 2 And s <> "the" And s <> "and" Then
        b = b + 1
        Range("B" & b) = s
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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