셀을 수동으로 복사 / 붙여 넣기 / 잘라 내기 / 삽입하면 문제가 발생하여 피하기 어렵습니다.
VBA 매크로를 통해 문제가 해결되었습니다.
셀을 수동으로 복사 / 붙여 넣기 / 잘라 내기 / 삽입하는 대신 셀 범위를 유지하는 Excel 매크로를 통해 수행합니다 (버튼을 통해 활성화 됨).
Sub addAndBtnClick()
Set Button = ActiveSheet.Buttons(Application.Caller)
With Button.TopLeftCell
ColumnIndex = .Column
RowIndex = Button.TopLeftCell.Row
End With
currentRowIndex = RowIndex
Set Table = ActiveSheet.ListObjects("Table name")
Table.ListRows.Add (currentRowIndex)
Set currentCell = Table.DataBodyRange.Cells(currentRowIndex, Table.ListColumns("Column name").Index)
currentCell.Value = "Cell value"
Call setCreateButtons
End Sub
Sub removeAndBtnClick()
Set Button = ActiveSheet.Buttons(Application.Caller)
With Button.TopLeftCell
ColumnIndex = .Column
RowIndex = Button.TopLeftCell.Row
End With
currentRowIndex = RowIndex
Set Table = ActiveSheet.ListObjects("Table name")
Table.ListRows(currentRowIndex - 1).Delete
End Sub
Sub setCreateButtons()
Set Table = ActiveSheet.ListObjects("Table name")
ActiveSheet.Buttons.Delete
For x = 1 To Table.Range.Rows.Count
For y = 1 To Table.Range.Columns.Count
If y = Table.ListColumns("Column name").Index Then
Set cell = Table.Range.Cells(x, y)
If cell.Text = "Some condition" Then
Set btn = ActiveSheet.Buttons.Add(cell.Left + cell.Width - 2 * cell.Height, cell.Top, cell.Height, cell.Height)
btn.Text = "-"
btn.OnAction = "removeAndBtnClick"
Set btn = ActiveSheet.Buttons.Add(cell.Left + cell.Width - cell.Height, cell.Top, cell.Height, cell.Height)
btn.Text = "+"
btn.OnAction = "addAndBtnClick"
End If
End If
Next
Next
End Sub
서식을 재설정하려면 (실제로 필요하지는 않음) :
Sub setCondFormat()
Set Table = ActiveSheet.ListObjects("Table name")
Table.Range.FormatConditions.Delete
With Table.ListColumns("Column name").DataBodyRange.FormatConditions _
.Add(xlExpression, xlEqual, "=ISTLEER(A2)") 'Rule goes here
With .Interior
.ColorIndex = 3 'Formatting goes here
End With
End With
...
End Sub