+ Reply to Thread
Results 1 to 5 of 5

Macro to repeat code on multiple rows

Hybrid View

  1. #1
    Registered User
    Join Date
    10-09-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Macro to repeat code on multiple rows

    Hi I have a Private Sub Macro that I am using currently that works. But I am new to this and need some help extending this.

    It currently clears the contents of Cells in the same row in Columns "H" through to "K" when the value in Column "B" is changed. i.e when cell B4 is changed cells H4:K4 are blanked out.

    I am looking for a code that will do this from rows 4 to 104 without having to write the code for each row (as in my current code)

    the current code I have is this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    
        For Each Cell In Target
            If Cell.Address = "$B$4" Then
                Application.EnableEvents = False
                    Range("H4:J4").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$5" Then
                Application.EnableEvents = False
                    Range("H5:J5").ClearContents
                Application.EnableEvents = True
            End If
            
             If Cell.Address = "$B$6" Then
                Application.EnableEvents = False
                    Range("H6:J6").ClearContents
                Application.EnableEvents = True
            End If
            
             If Cell.Address = "$B$7" Then
                Application.EnableEvents = False
                    Range("H7:J7").ClearContents
                Application.EnableEvents = True
            End If
            
             If Cell.Address = "$B$8" Then
                Application.EnableEvents = False
                    Range("H8:J8").ClearContents
                Application.EnableEvents = True
            End If
            
             If Cell.Address = "$B$9" Then
                Application.EnableEvents = False
                    Range("H9:J9").ClearContents
                Application.EnableEvents = True
            End If
            
             If Cell.Address = "$B$10" Then
                Application.EnableEvents = False
                    Range("H10:J10").ClearContents
                Application.EnableEvents = True
            End If
            
             If Cell.Address = "$B$11" Then
                Application.EnableEvents = False
                    Range("H11:J11").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$12" Then
                Application.EnableEvents = False
                    Range("H12:J12").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$13" Then
                Application.EnableEvents = False
                    Range("H13:J13").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$14" Then
                Application.EnableEvents = False
                    Range("H14:J14").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$15" Then
                Application.EnableEvents = False
                    Range("H15:J15").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$16" Then
                Application.EnableEvents = False
                    Range("H16:J16").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$17" Then
                Application.EnableEvents = False
                    Range("H17:J17").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$18" Then
                Application.EnableEvents = False
                    Range("H18:J18").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$19" Then
                Application.EnableEvents = False
                    Range("H19:J19").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$20" Then
                Application.EnableEvents = False
                    Range("H20:J20").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$21" Then
                Application.EnableEvents = False
                    Range("H21:J21").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$22" Then
                Application.EnableEvents = False
                    Range("H22:J22").ClearContents
                Application.EnableEvents = True
            End If
            
            If Cell.Address = "$B$23" Then
                Application.EnableEvents = False
                    Range("H23:J23").ClearContents
                Application.EnableEvents = True
            End If
            
        Next Cell
    
    
    End Sub

    Any Help would be much appreciated.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Macro to repeat code on multiple rows

    Hi,

    Try this :

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim cell As Range
      For Each cell In Target
          If cell.Column = 2 And cell.Row >= 4 And cell.Row <= 104 Then
             Application.EnableEvents = False
               cell.Offset(, 6).Resize(, 4).ClearContents
             Application.EnableEvents = True
          End If
      Next cell
    End Sub


    Regards
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    10-09-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Macro to repeat code on multiple rows

    That worked perfectly. Thank you very much.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,643

    Re: Macro to repeat code on multiple rows

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("B4:B104")) Is Nothing Then
            Application.EnableEvents = False
            Target.Offset(0, 6).Resize(columnsize:=3).ClearContents
            Application.EnableEvents = True
        End If
    
    End Sub
    Ben Van Johnson

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Macro to repeat code on multiple rows

    You are welcome, thanks for the feedback.


    Regards

+ 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] How to repeat a macro in multiple rows of a sheet?
    By rgoodman in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-01-2013, 09:07 AM
  2. [SOLVED] Tried to Build a Macro, but couldn't make it repeat for multiple rows.
    By homike2 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-06-2013, 09:37 AM
  3. Need macro help with transposing columns to rows and repeat a row value multiple times
    By InnovativeAJ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2012, 11:53 AM
  4. Repeat code for several rows with If statement
    By excel_vba_newbie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-28-2010, 06:37 PM
  5. Repeat code over 300 rows
    By Patrick Simonds in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-24-2005, 12:15 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