+ Reply to Thread
Results 1 to 7 of 7

Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    Hi All,

    I have the code below, but it takes a long time to complete because of the repetitive actions.
    Is there a way to make it more efficient?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim circRef As Range
    Dim c4 As Range
    Set circRef = Range("B32:B71")
    
    If Not Intersect(Target, circRef) Is Nothing Then
        For Each c4 In circRef
        Application.EnableEvents = False
        Application.ScreenUpdating = False
            If Not Target.Value = "SPARE" Then Exit Sub
            If c4.Value = "SPARE" Then
                c4.Offset(, 1).Value = "-"
                c4.Offset(, 3).Value = "-"
                c4.Offset(, 5).Value = "-"
                c4.Offset(, 7).Value = "-"
                c4.Offset(, 10).Value = "-"
                c4.Offset(, 13).Value = "-"
                c4.Offset(, 16).Value = "-"
                c4.Offset(, 25).Value = "-"
                c4.Offset(, 28).Value = "-"
                c4.Offset(, 30).Value = "-"
                c4.Offset(, 32).Value = "-"
                c4.Offset(, 38).Value = "-"
                c4.Offset(, 41).Value = "-"
                c4.Offset(, 45).Value = "-"
                c4.Offset(, 48).Value = "-"
                c4.Offset(, 51).Value = "-"
                c4.Offset(, 54).Value = "-"
                c4.Offset(, 57).Value = "-"
                c4.Offset(, 60).Value = "-"
                c4.Offset(, 63).Value = "-"
                c4.Offset(, 66).Value = "-"
                c4.Offset(, 69).Font.Name = "Arial"
                c4.Offset(, 69).Value = "-"
                c4.Offset(, 70).Value = "-"
                c4.Offset(, 74).Value = "-"
                c4.Offset(, 77).Font.Name = "Arial"
                c4.Offset(, 77).Value = "-"
                c4.Offset(, 78).Font.Name = "Arial"
                c4.Offset(, 78).Value = "-"
                c4.Offset(, 79).Value = "-"
            Else
                c4.Offset(, 1).Value = ""
                c4.Offset(, 3).Value = ""
                c4.Offset(, 5).Value = ""
                c4.Offset(, 7).Value = ""
                c4.Offset(, 10).Value = ""
                c4.Offset(, 13).Value = ""
                c4.Offset(, 16).Value = ""
                c4.Offset(, 25).Value = ""
                c4.Offset(, 28).Value = ""
                c4.Offset(, 30).Value = ""
                c4.Offset(, 32).Value = ""
                c4.Offset(, 38).Value = ""
                c4.Offset(, 41).Value = ""
                c4.Offset(, 45).Value = ""
                c4.Offset(, 48).Value = ""
                c4.Offset(, 51).Value = ""
                c4.Offset(, 54).Value = ""
                c4.Offset(, 57).Value = ""
                c4.Offset(, 60).Value = ""
                c4.Offset(, 63).Value = ""
                c4.Offset(, 66).Value = ""
                c4.Offset(, 69).Font.Name = "Wingdings 2"
                c4.Offset(, 69).Value = ""
                c4.Offset(, 70).Value = ""
                c4.Offset(, 74).Value = ""
                c4.Offset(, 77).Font.Name = "Wingdings 2"
                c4.Offset(, 77).Value = ""
                c4.Offset(, 78).Font.Name = "Wingdings 2"
                c4.Offset(, 78).Value = ""
                c4.Offset(, 79).Value = ""
                
            End If
        Next c4
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
    End Sub
    Any help would be greatly appreciated!

    Thanks,
    Storm08
    Attached Files Attached Files
    Last edited by Storm08; 11-09-2022 at 06:39 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,721

    Re: Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.

    Showing extracts of codes isn't as helpful as you might think. Problems raises more questions than answers.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    Thanks for the response.

    Sheet has been added to the original post.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,721

    Re: Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    This seems to work quicker:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim circRef As Range
    Dim c4 As Range
    Set circRef = Range("B32:B71")
    
    If Intersect(Target, circRef) Is Nothing Then Exit Sub
    On Error GoTo Error_Exit
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    For Each c4 In Intersect(Target, circRef)
        If c4.Value = "SPARE" Then
            c4.Offset(, 1).Resize(, 79).Value = "-"
            c4.Offset(, 69).Font.Name = "Arial"
            c4.Offset(, 77).Font.Name = "Arial"
            c4.Offset(, 78).Font.Name = "Arial"
        Else
            c4.Offset(, 1).Resize(, 79).Value = ""
            c4.Offset(, 69).Font.Name = "Wingdings 2"
            c4.Offset(, 77).Font.Name = "Wingdings 2"
            c4.Offset(, 78).Font.Name = "Wingdings 2"
        End If
    Next 'c4
    
    Error_Exit:
    If Err.Number <> 0 Then Debug.Print "ERROR"
    On Error GoTo 0
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    Hi TMS,

    Thanks for the solution.

    That definitely works a lot quicker than previously! However, is there a way to exclude some of the columns from the For loop?
    The original code doesn't apply it to each cell in the row, it skips some.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,721

    Re: Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    Difficult to assess. You have all those merged cells, always a (very, very) bad thing.

    The choices are:
    1. Loop through all the individual cells (original and slow)
    2. Fill all the cells in one go (as suggested and quicker)
    3. Combine the options. Group the cells by adjusting the offset and resize values.

    To be honest, when you refer to C4.Offset(,1) in relation to a merged cell, I’m not entirely sure which cell it means.

  7. #7
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: Insert "-" In Other Cells On The Same Row If Cell Value Is "SPARE"

    Hi TMS,

    Apologies for the severe delay!

    I know merged cells are generally bad news, but I can't do much to get away from them on this occasion.
    When you mention adjusting the offset/resize values, how would I go about trying this?

    +Rep!

    Cheers,
    Storm

+ 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. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. Replies: 3
    Last Post: 05-02-2018, 08:03 AM
  3. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  4. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  5. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  6. [SOLVED] Data validation: allow entry into a cell if other three cells have "X", "Y" and "Z"?
    By RogerRangeRover in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-05-2013, 04:49 AM
  7. Insert 19 cells "Shift to the right" if cell contains "->"
    By robertjtucker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-25-2005, 11:20 AM

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