I am new to VBA and try to handle a Sheet with 2 tables and specific macros. I created macros within one single module and placed to buttons called: Positionen_Einfügen (insert entire rows) and Zeile_Löschen (delete entire row).
The code runs perfect, but now I want to dilimit these macros for a specific area in my Wokrsheet(Einzelkosten), but the area is still flexible since you are allowed to insert multiple rows or delete one row.
In this case I placed an big red "Y" where the table stops. My "Y" is flexible and moves of course with the macros if you use them. Like several rows down or one up.
I want to use this "Y" as a boarder for the ActiveCell.EntireRow.Select. So can I write a .Find("Y") function within my macros, like in the code below:
Position_Einfügen()
'Disable Excel feautres to prevent Errors
ActiveSheet.Unprotect
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
'set specific range for area
Dim Target As Range
Set Target = Range("A9:R200").Find(Y, LookIn:=xlValues)
icountROws = Application.InputBox(Prompt:="How many rows do you want to insert after Line " _
& ActiveCell.Row & " ?", Type:=1)
' Dont allow negative numbers or empty field: Error Handling
If icountROws <= 0 Then End
ActiveCell.EntireRow.Select
'Can this work?
If ActiveCell.EntireRow.Select >= Y And ActiveCell.EntireRow.Select = Y Then
MsgBox ("Sie befinden sich außerhalb des erlaubten Bereichs")
End If
Exit Sub
Else If
Selection.Copy
' Selection.PasteSpecial xlPasteFormulas
Rows(ActiveCell.Row & ":" & ActiveCell.Row + icountROws - 1).Insert shift:=xlDown
End If
'Re-enable features after running macro, auto-debugging
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
True, AllowFormattingCells:=True, AllowFormattingColumns:=True
ActiveSheet.EnableSelection = xlNoRestrictions
End Sub
Here is the 2. macro: Delete Function
Sub Zeile_Löschen()
'select row to delete
Dim DeletePrompt As Integer
DeletePrompt = MsgBox("Are you sure you want to delete this row?", vbYesNo + vbQuestion, "Delete")
ActiveSheet.Unprotect
If DeletePrompt = vbYes Then
Rows(ActiveCell.Row).Delete
Else
'do nothing
End If
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
True, AllowFormattingCells:=True, AllowFormattingColumns:=True
ActiveSheet.EnableSelection = xlNoRestrictions
End Sub