Reference one cell to another sheet while applying VBA code.

noveske

Board Regular
Joined
Apr 15, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. 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.

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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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