Target a range with existing VBA code.

noveske

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

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
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I may be able to help but would prefer you explain in detail what your trying to accomplish. I'm not good at reading a whole lot of code that does not work and try to fix it so it will do something.
Thanks
 
Upvote 0
I may be able to help but would prefer you explain in detail what your trying to accomplish. I'm not good at reading a whole lot of code that does not work and try to fix it so it will do something.
Thanks
Currently this is placed in ThisWorkbook.
Instead of the code applying to the entire sheet that's active, I want it to apply only to certain cells.

Like: A1:B5.
 
Upvote 0
maybe this:
VBA Code:
If Target.Cells.Count > 1 or intersect(target,range("A1:B5")) is nothing Then Exit Sub
 
Upvote 0
Currently this is placed in ThisWorkbook.
Instead of the code applying to the entire sheet that's active, I want it to apply only to certain cells.

Like: A1:B5.
You still did not explain what this script is attempting to do.
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,641
Members
449,177
Latest member
Sousanna Aristiadou

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