View Single Post
 
Old 08-28-2017, 09:20 PM
NoSparks NoSparks is offline Windows 7 64bit Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

try this, hopefully it's commented enough that you can follow

Code:
Sub YourExistingMacro()
    'declare variables
    Dim lr As Long          'last row used in the column
    Dim rng As Range        'the range to operate on
    Dim cel As Range        'individual cells of that rng
    Dim ws As Worksheet     'the worksheet being worked on
    Dim a As Variant        'array to hold individual words of the cell
    
    '
    '~~~~~~~~~~~~~~~~~~~~~~~~~
    ' your existing code here
    '~~~~~~~~~~~~~~~~~~~~~~~~~
    '
    
Set ws = Sheets("Sheet1")
With ws
    'determine the last row of column I
    lr = .Range("I" & Rows.Count).End(xlUp).Row
    'establish the range to operate on
    Set rng = .Range("I2:I" & lr)
    'step through the range one cell at a time
    For Each cel In rng
        'provided the cell isn't blank
        If cel.Value <> "" Then
            'load the individual words into array
            a = Split(cel.Value, " ")
            'write the first element of the array one column right of cel
            cel.Offset(, 1).Value = a(LBound(a))
            'write the last element of the array two columns right of cel
            cel.Offset(, 2).Value = a(UBound(a))
        End If
    'move to the next cell
    Next cel
End With
    
End Sub
Reply With Quote