Macro Cancellazione dati celle in un foglio di lavoro Excel

Eseguire la seguente routine “CancellaFoglio” per eliminare tutti i dati del foglio di lavoro “Test”

Sub CancellaFoglio()

  Worksheets("Test").Range(Cells(1, 1), Cells(GetRealLastCellRow("Test"), GetRealLastCellColumn("Test"))).ClearContents

End Sub

'Funzione che recupera l'indice dell'ultima riga del foglio di lavoro
Function GetRealLastCellRow(p_Worksheets As String) As Long

  Dim lRealLastRow As Long
  
  Worksheets(p_Worksheets).Range("A1").Select
  On Error Resume Next
  lRealLastRow = Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, _
                                               xlPrevious).Row
                                                 
  GetRealLastCellRow = lRealLastRow
End Function

'Funzione che recupera l'indice dell'ultima colonna del foglio di lavoro
Function GetRealLastCellColumn(p_Worksheets As String) As Long
  
  Dim lRealLastColumn As Long
  Worksheets(p_Worksheets).Range("A1").Select
  On Error Resume Next
  lRealLastColumn = Cells.Find("*", Range("A1"), xlFormulas, , _
                                    xlByColumns, xlPrevious).Column
  GetRealLastCellColumn = lRealLastColumn
End Function
This entry was posted in VBA Macro. Bookmark the permalink.