Consulting

Results 1 to 6 of 6

Thread: Repeat VBA code for each cell

  1. #1
    VBAX Regular
    Joined
    Dec 2017
    Posts
    12
    Location

    Repeat VBA code for each cell

    Hello,

    I have this code that works for the cell intended but i would like it available for a range of cell C4 thru C103.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C4")) Is Nothing Then
    If Range("C4") = "" Then Range("D4:V4").ClearContents
    If Range("C4") <> "" Then Range("D4:V4").ClearContents
    End If
    End Sub
    Any help would be grateful

    Thanks
    Jeffrey
    Last edited by Paul_Hossler; 08-16-2018 at 08:11 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    1. You should use the[#] icon to insert CODE tags and then paste ypur macro between them

    2. These don't make sense since D-V get cleared regardless of the C
    If Range("C4") = "" Then Range("D4:V4").ClearContents If Range("C4") <> "" Then Range("D4:V4").ClearContents

    3. You should disable event handling when you're changing the WS from inside an event handler


    4. GUESSING that you want D-V changed in the row where C is. This clears D-V regardless of the contents of the corresponding C

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
        
        If Intersect(Target, Range("C4:C103")) Is Nothing Then Exit Sub
    
    
        Application.EnableEvents = False
        For Each r In Intersect(Target, Range("C4:C103")).Cells
            r.Offset(0, 1).Resize(1, 19).ClearContents
        Next
        Application.EnableEvents = True
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Dec 2017
    Posts
    12
    Location
    this is why.
    it may be incorrect but it seems to works.


    If Range("C4") = "" Then Range("D4:V4").ClearContents (With is one if i use the delete key it clears my range but if i change the value of C4 it won't clear the range.)
    If Range("C4") <> "" Then Range("D4:V4").ClearContents
    (With is one if i use the if i change the value of C4 it clears the range but if i use delete key it won't clear my range.)

  4. #4
    VBAX Regular
    Joined
    Dec 2017
    Posts
    12
    Location
    Thank You!

    but i would like to add a cell to clear. I see you are clearing 1 thru 19 cells after column C i also need to skip 20, 21 and clear 22.

    how would i add?

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    either add directly after the
    r.Offset(0, 1).Resize(1, 19).ClearContents
    line, the following:
    r.Offset(, 22).ClearContents

    or
    replace it with:
    Union(r.Offset(, 1).Resize(, 19), r.Offset(, 22)).ClearContents
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Regular
    Joined
    Dec 2017
    Posts
    12
    Location
    Awesome!!
    Thank You!

Posting Permissions

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