noveske
Board Regular
- Joined
- Apr 15, 2022
- Messages
- 120
- Office Version
- 365
- Platform
- Windows
- Mobile
- Web
Just need to have this code target only certain cells on the sheet. I tried added range to a few places and was not able to get it to work.
When it did work, it partially worked, but would interact incorrectly and move it somewhere I couldn't find it.
When it did work, it partially worked, but would interact incorrectly and move it somewhere I couldn't find it.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r%, C%, x%, iPos%, iOffset%, length%
Dim strOriginal$, strExtract$
Const CHARS_COUNT% = 100
If Target.Cells.Count > 1 Then Exit Sub
On Error GoTo MACROS_FAIL
Application.EnableEvents = False
strOriginal = Target.Value
length = Len(strOriginal)
r = Target.Row - 1: C = Target.Column
iPos = 1
If length > CHARS_COUNT Then
While iPos <= length
nextPos = WorksheetFunction.Min(length, iPos + CHARS_COUNT)
prev_sp = InStrRev(strOriginal, " ", nextPos)
If nextPos < length Then
next_sp = InStr(nextPos, strOriginal, " ")
Else
next_sp = nextPos
End If
If next_sp = 0 Then next_sp = nextPos
If (next_sp - nextPos) < (nextPos - prev_sp) Then
nextPos = next_sp
Else
nextPos = prev_sp
End If
strExtract = Trim(Mid$(strOriginal, iPos, nextPos - iPos + 1))
x = x + 1
If x > 3 Then
x = 0: iOffset = 1
Else
iOffset = 1
End If
r = r + iOffset
Cells(r, C) = strExtract
iPos = nextPos + 1
Wend
End If
Application.EnableEvents = True
Exit Sub
MACROS_FAIL:
Application.EnableEvents = True
MsgBox "Error:" & Chr(10) & Err.Description, vbCritical
End Sub