+ Reply to Thread
Results 1 to 16 of 16

Clear cell range on multiple sheets if cell on one sheet is modified.

Hybrid View

  1. #1
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Clear cell range on multiple sheets if cell on one sheet is modified.

    The script below works perfect, except I need it to clear contents on worksheets labeled "Monday, Tuesday, Wednesday, Thursday, Friday" when cell (U2) on "Monday" is changed. HELP!!!


    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("u2")) Is Nothing Then Exit Sub
    [H8:O27,H30:O41,U30:U41].ClearContents
    End Sub

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Welcome to the board!

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Worksheets("Monday").Range("u2")) Is Nothing Then
        Dim WSNames as Variant
        Dim A as Long
        WSNames = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
        For A = 0 To UBound(WSNames)
            Worksheets(WSNames(A)).Range("H8:O27", "H30:O41", "U30:U41").ClearContents
        Next
    End Sub
    Last edited by Tinbendr; 07-05-2012 at 01:58 PM.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    David,

    Thanks for your response, however I am now getting a compile error:
    "Block If without End If"

    I know this is basic debugging but I am super new to VBA and need this spoon fed to me. Sorry and Thanks.

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Oops! Copy/Paste error.
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Worksheets("Monday").Range("u2")) Is Nothing Then
        Dim WSNames as Variant
        Dim A as Long
        WSNames = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
        For A = 0 To UBound(WSNames)
            Worksheets(WSNames(A)).Range("H8:O27", "H30:O41", "U30:U41").ClearContents
        Next
    End If
    End Sub

  5. #5
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Now I'm getting a "Run-time error '450': Wrong number of arguments or invalid property assignment" I have attached a copy of the workbook. Please see attached.

    Thanks.
    blank template (1 day) v4.2.xls

  6. #6
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    oops wrong file. I forgot to save changes. try this one
    blank template (1 day) v4.2.xls

  7. #7
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    It doesn't like the quotes mid-stream on the Range argument:

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, Range("u2")) Is Nothing Then
            Dim WSNames As Variant
            Dim A As Long
        
            Application.EnableEvents = False
            WSNames = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
            For A = LBound(WSNames) To UBound(WSNames)
                Worksheets(WSNames(A)).Range("H8:O27,H30:O41,U30:U41").ClearContents
            Next
            Application.EnableEvents = False
        End If
    
    End Sub

  8. #8
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Quote Originally Posted by wallyeye View Post
    It doesn't like the quotes mid-stream on the Range argument:

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, Range("u2")) Is Nothing Then
            Dim WSNames As Variant
            Dim A As Long
        
            Application.EnableEvents = False
            WSNames = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
            For A = LBound(WSNames) To UBound(WSNames)
                Worksheets(WSNames(A)).Range("H8:O27,H30:O41,U30:U41").ClearContents
            Next
            Application.EnableEvents = False
        End If
    
    End Sub


    Thanks a million It works like a charm.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Try this
    
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("u2")) Is Nothing Then Exit Sub
        Sheets(Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")).Select
        Me.Range("H8:O27,H30:O41,U30:U41").Select
        Selection.ClearContents
    Me.Range("A1").Select
    End Sub
    Last edited by royUK; 07-05-2012 at 02:38 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  10. #10
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Is there a way to ungroup them when it is done clearing the contents, because if I input any data into any sheet in the group it copies the data to the other sheets.

  11. #11
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Thank you everyone!!!

  12. #12
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Follow up,

    I submitted this unprotected, but when I password protected the sheet it will no longer work. I'm getting "Run-time error '1004': The cell or chart that you are trying to change is protected and therefore read-only. To modify a protected cell or chart, first remove protection using the Unprotect Sheet command (Review tab, Changes group). You may be prompted for a password." Can you please amend the code below to compensate for this?

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("u2")) Is Nothing Then
    Dim WSNames As Variant
    Dim A As Long

    Application.EnableEvents = False
    WSNames = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Vacation", "Holiday", "Funeral", "Jury")
    For A = LBound(WSNames) To UBound(WSNames)
    Worksheets(WSNames(A)).Range("H8:O27,H30:O41,U30:U41").ClearContents
    Next
    Application.EnableEvents = False
    End If

    End Sub

  13. #13
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    I have a sheet protection function I use, it allows me to store the protection password in one location, rather than everywhere I need to protect/unprotect the sheet:

    Option Explicit
    
    Public Sub SheetProtect(Optional wksCurr As Variant)
    On Error GoTo Proc_Error
    '
    '  Apply worksheet protection.  If a sheet is passed, protect only that sheet,
    '    else protect all sheets
    '
        Dim strPW As String
    
        strPW = "mypass"
        If IsMissing(wksCurr) Then
            For Each wksCurr In ThisWorkbook.Worksheets
                wksCurr.Protect (strPW)
            Next wksCurr
            Set wksCurr = Nothing
        Else
            If VarType(wksCurr) = vbObject Then
                If TypeOf wksCurr Is Worksheet Then
                    wksCurr.Protect (strPW)
                End If
            End If
        End If
    
    Proc_Exit:
    
        Exit Sub
    
    Proc_Error:
    
        Select Case Err
            Case Else
                MsgBox "Error " & CStr(Err) & ": " & Err.Description
                Resume Proc_Exit
        End Select
    
        Exit Sub
    
    End Sub
    
    Public Function SheetUnprotect(Optional wksCurr As Variant) As Boolean
    On Error GoTo Proc_Error
    '
    '  Apply worksheet protection.  If a sheet is passed, unprotect only that sheet,
    '    else unprotect all sheets
    '
        Dim strPW As String
    
        strPW = "mypass"
        If IsMissing(wksCurr) Then
            For Each wksCurr In ThisWorkbook.Worksheets
                wksCurr.Unprotect (strPW)
            Next wksCurr
            Set wksCurr = Nothing
        Else
            If VarType(wksCurr) = vbObject Then
                If TypeOf wksCurr Is Worksheet Then
                    SheetUnprotect = wksCurr.ProtectContents
                    wksCurr.Unprotect (strPW)
                End If
            End If
        End If
    
    Proc_Exit:
    
        Exit Function
    
    Proc_Error:
    
        Select Case Err
            Case Else
                MsgBox "Error " & CStr(Err) & ": " & Err.Description
                Resume Proc_Exit
        End Select
    
        Exit Function
    
    End Function
    Put it in a new module, change the password to match yours, then change the routine:

            For A = LBound(WSNames) To UBound(WSNames)
                SheetUnprotect(worksheets(wsnames(A)))
                Worksheets(WSNames(A)).Range("H8:O27,H30:O41,U30:U41").ClearContents
                SheetProtect(worksheets(wsnames(A)))
            Next
    You can add parameters to the .Protect statement, check the help files. I had some spreadsheets I support that had 40-50 locations that protected/unprotected the worksheets, this function cut down on maintenance quite a bit. You can also pass worksheets identified with their codenames to it.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    I added the ungroup line, it should be


    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("u2")) Is Nothing Then Exit Sub
        Sheets(Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")).Select
        Me.Range("H8:O27,H30:O41,U30:U41").Select
        Selection.ClearContents
        Me.Select
    End Sub
    To get round the the protection issue you can use Protect with UserInterface, that allows macros to work on protected sheets.
    Last edited by royUK; 07-06-2012 at 02:10 AM.

  15. #15
    Registered User
    Join Date
    07-05-2012
    Location
    Delaware
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    Hello everyone,

    I'm back with a follow up modification request.

    I would like to replace the worksheet names "Monday, etc" and set this script to clear the cells throughout the entire workbook. Can anyone help me?

    Thanks a million in advance.

  16. #16
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Clear cell range on multiple sheets if cell on one sheet is modified.

    I don't know how to select all the worksheets in a dynamic array like the previous example, this loops through each worksheet:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim wksCurr                 As Worksheet
    
        Dim intCalc                 As Integer
    
        If Intersect(Target, Range("u2")) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        intCalc = Application.Calculation
        Application.Calculation = xlCalculationManual
        For Each wksCurr In ThisWorkbook.Worksheets
            wksCurr.Range("H8:O27,H30:O41,U30:U41").ClearContents
        Next wksCurr
        Application.EnableEvents = True
        Application.Calculation = intCalc
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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