+ Reply to Thread
Results 1 to 8 of 8

How to make VBA code only apply to a range of cells

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2017
    Location
    California, US
    MS-Off Ver
    Office 2016
    Posts
    38

    How to make VBA code only apply to a range of cells

    I would like for the VBA code below to only effect a range of cells rather than the entire worksheet. I have tried manipulation and a few different things with no success. What makes sense to me is to change the line "With Target.Cells(1)" but that did not work.

    Please help!

    Option Explicit
    Option Compare Text
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim r                As Excel.Range
       Dim StartPos         As Long
    
       With Target.Cells(1)
          If .Value <> vbNullString Then
    
             Application.EnableEvents = False
             For Each r In Sheet1.UsedRange.Columns(1).Cells
    
                If r.Value <> vbNullString Then
                   If InStr(.Value, r.Value) > 0 Then
                      .Value = Replace(.Value, r.Value, r.Offset(, 1).Value)
                      .Characters(InStr(.Value, r.Offset(, 1).Value), 1).Font.Name = r.Offset(, 1).Font.Name
                   End If
                End If
             Next
             Application.EnableEvents = True
          End If
       End With
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How to make VBA code only apply to a range of cells

    Perhaps this...
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim r                As Excel.Range
       Dim ChangedCell      As Excel.Range
       Dim StartPos         As Long
    
       On Error GoTo Catch
       
       Application.EnableEvents = False
       
       For Each ChangedCell In Intersect(Range("A10:C50"), Target)
    
          With ChangedCell
             If .Value <> vbNullString Then
    
                For Each r In Sheet2.UsedRange.Columns(1).Cells
    
                   If r.Value <> vbNullString Then
                      If InStr(.Value, r.Value) > 0 Then
                         .Value = Replace(.Value, r.Value, r.Offset(, 1).Value)
                         .Characters(InStr(.Value, r.Offset(, 1).Value), 1).Font.Name = r.Offset(, 1).Font.Name
                      End If
                   End If
                Next
             End If
          End With
       Next
       
    Catch:
    
       Application.EnableEvents = True
    
    End Sub
    With the range 'A10:C50 edited as needed.

  3. #3
    Registered User
    Join Date
    02-06-2017
    Location
    California, US
    MS-Off Ver
    Office 2016
    Posts
    38

    Re: How to make VBA code only apply to a range of cells

    Perfect!!! Thank you!!

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: How to make VBA code only apply to a range of cells

    Hello anthony777,

    Please could you be more specific regarding the actual outcome of the result, and where and how it should be like!

    Maybe it would be I good idea to attach a sample workbook.

    Make sure there is just enough data to demonstrate your need.

    Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate.

    Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

    Thank you.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Registered User
    Join Date
    02-06-2017
    Location
    California, US
    MS-Off Ver
    Office 2016
    Posts
    38

    Re: How to make VBA code only apply to a range of cells

    Thank you for the feedback in the future I will be sure to be clear and concise.

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: How to make VBA code only apply to a range of cells

    Hi anthony777,

    I am still in the dark!

    Please could you show me the difference? Maybe I am just tired, and everything appears as clear as mud.(:

    Spank you so much!

  7. #7
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How to make VBA code only apply to a range of cells

    @Winon,

    This is a follow on question from another thread - might help to unmuddy the waters...

  8. #8
    Registered User
    Join Date
    02-06-2017
    Location
    California, US
    MS-Off Ver
    Office 2016
    Posts
    38

    Re: How to make VBA code only apply to a range of cells

    @Winon

    For some reason the attachment option is not working for me.

    What I am trying to do here is automatically substitute symbols for specific text. That VBA code was provided in a previous post, but I want this VBA to only effect a range of cells "D9:D49" for example.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VB code to apply Conditional formatting to a range of cells
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-10-2016, 11:49 AM
  2. VBA code that will re-apply my formula to the specific range of cells
    By glenn0004 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-29-2015, 08:49 AM
  3. [SOLVED] How to make a singular macro apply to multiple cells?
    By Lowa in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-18-2015, 10:26 AM
  4. Loop through a range of names and apply each name to my code
    By papasmurfuo9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2015, 10:36 AM
  5. how make vba code work over range of cells
    By sspreyer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2013, 11:42 PM
  6. [SOLVED] Code to apply for a certain range only.
    By loekie in forum Excel General
    Replies: 2
    Last Post: 05-17-2012, 04:20 AM
  7. Replies: 1
    Last Post: 02-22-2012, 12:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1