I'm trying to write a sub that will delete the contents of a row and move the contents of everything below that row up to fill the gap.
I want to apply the same macro to each command button on the spreadsheet, at creation, so I wanted to write the macro such that it gets all its position information from the action of clicking the button that calls the macro.
For some reason, it just started giving me an "Unable to get the TopLeftCell property of the Button class" error (it used to work pretty okay, but somehow I broke it as I was trying to fix it).
What am I doing wrong?
Here is my code
Sub DelTask()
Dim btn As Object 'variable to be used as the button pressed
Dim btnadd As String 'variable for the address of the pressed button
Dim tsk As Range 'Variable for the location of the task name
Dim tskadd As String
Dim btnRow As Integer 'variable to hold the row of the button pressed
Dim delcell As Range
Dim shftcell As Range
Dim shftcells As String
NumTasks = Application.WorksheetFunction.CountA(Sheets("Tasks").Range("B7:B10000"))
Set btn = ActiveSheet.Buttons(Application.Caller)
With btn.TopLeftCell
btnadd = .Address
btnRow = .Row
End With
Set tsk = Sheets("Tasks").Range(btnadd).Offset(0, -11)
tskadd = Sheets("Tasks").Range(btnadd).Offset(0, -11).Address
Here is the code from the macro that creates the buttons in the first place:
Dim CaseyCell As Range 'Range Variable
Dim CellAdd As String 'variable to hold address of cell
Dim CaseyDel As Range 'Range variable for delete checkbox
Dim CellDel As String 'variable to hold address of delete checkbox
Dim btn As String
Set CaseyCell = Sheets("Tasks").Range("ToDo_Start").Offset(NumTasks + 1, 10)
Set CaseyDel = Sheets("Tasks").Range("ToDo_Start").Offset(NumTasks + 1, 11)
'CaseyCell.Select
CellAdd = CaseyCell.Address(False, False)
CellDel = CaseyDel.Address(False, False)
If New_Edit = "New" Then
With ActiveSheet.CheckBoxes.Add(CaseyCell.Left, CaseyCell.Top, CaseyCell.Width, CaseyCell.Height)
' .name = "Chkbx" & CellAdd
.Caption = ""
.LinkedCell = CellAdd
.Placement = xlFreeFloating
.OnAction = "CrossOutRow"
End With
With ActiveSheet.Buttons.Add(CaseyDel.Left, CaseyDel.Top, CaseyDel.Width, CaseyDel.Height)
.Name = "Button" & CellDel
.Caption = "DELETE TASK"
.Characters.Font.Name = "Calibri"
.Characters.Font.Size = 12
.Characters.Font.StrikeThrough = False
.Characters.Font.Superscript = False
.Characters.Font.Subscript = False
.Characters.Font.Shadow = False
.Characters.Font.Underline = xlUnderlineStyleNone
.Characters.Font.ColorIndex = xlAutomatic
.OnAction = "DelTask"
End With
End If