Consulting

Results 1 to 3 of 3

Thread: Clear contents in multiple range and multiple sheets

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Clear contents in multiple range and multiple sheets

    Why i keep on having a Run-time error 1004 "Cannot change a part of a merge cell" and besides I do not have any merge cell in the sheets instead of Data Validation? is this considered as merge cell?

    Private Sub CommandButton1_Click()
    
    
    Dim Ws As Worksheet, rMyRg As Range
    
    
    Set rMyRg = Range("d1:d54,f1:q54")
    
    
     
    For Each Ws In Worksheets
    UnProtectActiveSheet
        Select Case Ws.Name
        Case "Macro_Settings", "Master", "TOC", "Summary"
             ' do nthing
        Case Else
        Application.ScreenUpdating = False
            rMyRg.ClearContents
        End Select
        Application.ScreenUpdating = True
    Next Ws
    
    
    ProtectActiveSheet
    
    
    End Sub
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    VBAX Regular raj85's Avatar
    Joined
    Feb 2010
    Location
    Mumbai
    Posts
    34
    Location
    please share worksheet

  3. #3
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Private Sub Co()
    Dim Ws As Worksheet, rMyRg As Range
    Set rMyRg = Range("d1:d54,f1:q54")
        
        
    Call Protect_Unprotect_All 'toggle on/off
        
    For Each Ws In Sheets(Array(Sheet1.Name, Sheet2.Name, Sheet3.Name))
        If Ws.ProtectContents = False Then
            rMyRg.ClearContents
        End If
    Next Ws
    
    Call Protect_Unprotect_All
        
      
        
    End Sub
    
    Sub Protect_Unprotect_All()
    Dim wSheet As Worksheet
    Dim Password As String * 3
    Application.ScreenUpdating = False
    Password = "xyz" ' your Password here or "" for no password
    For Each wSheet In Worksheets
        With wSheet
            If .ProtectContents = True Then
                .Unprotect Password
            Else
                .Protect Password, _
                 DrawingObjects:=True, _
                 Contents:=True, _
                 Scenarios:=True, _
                 AllowSorting:=True, _
                 AllowFormattingCells:=True, _
                 AllowFiltering:=True, _
                 AllowUsingPivotTables:=True
            End If
        End With
     Next wSheet
     Application.ScreenUpdating = True
    End Sub
    "To a man with a hammer everything looks like a nail." - Mark Twain

Posting Permissions

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