Hi all,
I was hoping someone might be able to assist with the following as I am a bit of an Excel macro novice.
I am trying to create a spreadsheet where users fill out the details in grey to create a budget. I have protected the sheet so users can only edit the cells in grey.
To make this spreadsheet as user friendly as possible, I have added 2 images in column G and assigned macros to them so users can copy or delete that row.
So when I click on the copy image in row 11, the macro copies the details into an inserted row below and the user then edits as required.
Then if I click the copy image in row 12, this copies those details into row 13 and the process continues.
The issue though is the row copy seems based on where the selected cell is, and nothing to do with the row the button is clicked on.
So in the screenshot below, I click on the copy image in row 12 but as the highlighted cell was B11, it copies those details from row 11 down (which would throw people a little).
Likewise (and more worryingly) for the delete button, if I wanted to delete row 13 but the highlighted cell was in row 12, the user unwittingly deletes the wrong person.
So what I think I need is for the macro to detect the row of the clicked image, and then set the focus to highlight a cell on that row, but I am having no joy in working out how.
My macros are as below:
Sub AddRowPersonnel()
'Take worksheet out of protected mode
ActiveSheet.Unprotect
'Select the existing row and copy it
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
'Move cursor down one row, insert new row, and then paste values into the new row
ActiveCell.Offset(1, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.EntireRow.INSERT
ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
Selection.Copy
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
ActiveSheet.Paste
'Put worksheet back into protected mode
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Sub DeleteRowPersonnel()
‘ Unprotect worksheet
ActiveSheet.Unprotect
‘ Highlight selected row and delete it
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp
‘Put worksheet back into protected mode
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Many thanks in advance!
I was hoping someone might be able to assist with the following as I am a bit of an Excel macro novice.
I am trying to create a spreadsheet where users fill out the details in grey to create a budget. I have protected the sheet so users can only edit the cells in grey.
To make this spreadsheet as user friendly as possible, I have added 2 images in column G and assigned macros to them so users can copy or delete that row.
So when I click on the copy image in row 11, the macro copies the details into an inserted row below and the user then edits as required.
Then if I click the copy image in row 12, this copies those details into row 13 and the process continues.
The issue though is the row copy seems based on where the selected cell is, and nothing to do with the row the button is clicked on.
So in the screenshot below, I click on the copy image in row 12 but as the highlighted cell was B11, it copies those details from row 11 down (which would throw people a little).
Likewise (and more worryingly) for the delete button, if I wanted to delete row 13 but the highlighted cell was in row 12, the user unwittingly deletes the wrong person.
So what I think I need is for the macro to detect the row of the clicked image, and then set the focus to highlight a cell on that row, but I am having no joy in working out how.
My macros are as below:
Sub AddRowPersonnel()
'Take worksheet out of protected mode
ActiveSheet.Unprotect
'Select the existing row and copy it
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
'Move cursor down one row, insert new row, and then paste values into the new row
ActiveCell.Offset(1, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.EntireRow.INSERT
ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
Selection.Copy
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
ActiveSheet.Paste
'Put worksheet back into protected mode
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Sub DeleteRowPersonnel()
‘ Unprotect worksheet
ActiveSheet.Unprotect
‘ Highlight selected row and delete it
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp
‘Put worksheet back into protected mode
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Many thanks in advance!