noveske
Board Regular
- Joined
- Apr 15, 2022
- Messages
- 120
- Office Version
- 365
- Platform
- Windows
- Mobile
- Web
I'm trying to enter a large amount of text into one large cell (Sheet1!A20).
Then reference it to another sheet (Sheet2!A50).
Then apply VBA code to Sheet2. Only apply to (Sheet2!A50:A60) if possible.
Currently VBA code is on Sheet1 and applies to everything over 110 characters then breaks to the next row keeping full words intact.
I've tried moving the code to Sheet2 and attempted to add a range but failed.
Thank you for your time and help.
Then reference it to another sheet (Sheet2!A50).
Then apply VBA code to Sheet2. Only apply to (Sheet2!A50:A60) if possible.
Currently VBA code is on Sheet1 and applies to everything over 110 characters then breaks to the next row keeping full words intact.
I've tried moving the code to Sheet2 and attempted to add a range but failed.
Thank you for your time and help.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r%, c%, x%, iPos%, iOffset%, length%
Dim strOriginal$, strExtract$
Const CHARS_COUNT% = 110
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