Add exception in Cell.Delete

Asked

Viewed 42 times

0

I was using this Sub Clearcontents to clean up all the content I had in the Sheets, I need to add an exception that is the G1 cell and I found this other Sub, but I don’t know how to mix the two

Sub ClearContents()

 Application.Calculation = xlManual
 Application.ScreenUpdating = False

  Sheets("01-Janeiro").Cells.Delete
  Sheets("02-Fevereiro").Cells.Delete
  Sheets("03-Março").Cells.Delete
  Sheets("04-Abril").Cells.Delete
  Sheets("05-Maio").Cells.Delete
  Sheets("06-Junho").Cells.Delete
  Sheets("07-Julho").Cells.Delete
  Sheets("08-agosto").Cells.Delete
  Sheets("09-Setembro").Cells.Delete
  Sheets("10-Outubro").Cells.Delete
  Sheets("11-Novembro").Cells.Delete
  Sheets("12-Dezembro").Cells.Delete

 Application.ScreenUpdating = True
End Sub


Sub ClearAllExceptSelection()
'updateby Extendoffice 20151113
    Dim xRg As Range
    Dim xCell As Range
    Dim xAddress As String
    Dim xUpdate As Boolean
    On Error Resume Next
    xAddress = Application.ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select the ranges want to keep", "Kutools for Excel", xAddress, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    For Each xCell In ActiveSheet.UsedRange
        If Intersect(xCell, xRg) Is Nothing Then
            xCell.Clear
        End If
    Next
    Application.ScreenUpdating = xUpdate
End Sub

I need to erase everything in the sheet and keep G1 intact and if you also know how to apply it in this function would help me

Public Function SheetClear(Name As String)
Dim s As Worksheet, t As String
Dim i As Long, k As Long
k = ThisWorkbook.Sheets.Count

For i = k To 1 Step -1
    t = ThisWorkbook.Sheets(i).Name
    If t = Name Then
        Application.DisplayAlerts = False
        ThisWorkbook.Sheets(i).Cells.Delete
        Application.DisplayAlerts = True
    End If
Next i

End Function

1 answer

1


Tip: Preserve G1 cell content in a variable, clear all and then return the value to the cell.

Public Function SheetClear(Name As String)
Dim s As Worksheet, t As String
Dim i As Long, k As Long

Dim CelulaG1 As String 'Ou double, ou integer, depende do tipo de dado que está em G1

    k = ThisWorkbook.Sheets.Count
    For i = k To 1 Step -1
        t = ThisWorkbook.Sheets(i).Name
        If t = Name Then
            Application.DisplayAlerts = False
                CelulaG1 = Sheets(i).Range("G1").Value
                ThisWorkbook.Sheets(i).Cells.Delete
                Sheets(i).Range("G1").Value = CelulaG1
            Application.DisplayAlerts = True
        End If
    Next i
End Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.