Martin sherk
Board Regular
- Joined
- Sep 11, 2022
- Messages
- 94
- Office Version
- 365
- 2016
I use the below code to update a used range and name it, I need a way to automate this process when users delete data in that sheet and add new data, I want VBA to be able to automatically name that new used range without having to run the code every time.
VBA Code:
Sub update()
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set sht = Worksheets("SAP")
Set StartCell = Range("A1")
'Refresh UsedRange
Worksheets("SAP").UsedRange
'Find Last Row and Column
LastRow = StartCell.SpecialCells(xlCellTypeLastCell).Row
LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column
'Select Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Name = "MyData"
End Sub