Consulting

Results 1 to 7 of 7

Thread: Solved: Insert/Delete Row Between Entry

  1. #1
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location

    Solved: Insert/Delete Row Between Entry

    Hi,

    this is the currently design of my fund transfer template.. the worksheet is protected - the password is "password"..

    i need to insert/delete a row between entry (for ex: insert feb 11 between feb 10 and 12) or whichever the user wants to insert/delete a row..


    Thanks..

  2. #2
    VBAX Regular
    Joined
    Oct 2006
    Posts
    9
    Location

    Try this one

    Hi,

    try these ones:

    [VBA]Option Explicit
    Public Const strPassword As String = "password"
    Sub InsertRow()
    Dim intFinalRow As Integer

    Application.ScreenUpdating = False

    intFinalRow = Cells(65536, 1).End(xlUp).Row

    ActiveSheet.Unprotect strPassword
    With Range("A" & intFinalRow)
    .EntireRow.Insert
    .Offset(-2, 0).EntireRow.Copy
    .Offset(-1, 0).PasteSpecial xlPasteAll
    End With
    ActiveSheet.Protect strPassword

    Application.ScreenUpdating = True
    End Sub
    Sub InsertBetweenRow()
    Dim intActiveRow As Integer

    Application.ScreenUpdating = False

    intActiveRow = ActiveCell.Row

    ActiveSheet.Unprotect strPassword
    With Range("A" & intActiveRow)
    .EntireRow.Insert
    .Offset(-2, 0).EntireRow.Copy
    .Offset(-1, 0).PasteSpecial xlPasteAll
    End With
    ActiveSheet.Protect strPassword

    Application.ScreenUpdating = True
    End Sub
    Sub DeleteRow()
    Dim intActiveRow As Integer

    Application.ScreenUpdating = False

    intActiveRow = ActiveCell.Row

    ActiveSheet.Unprotect strPassword
    With Range("A" & intActiveRow)
    .EntireRow.Delete
    End With
    ActiveSheet.Protect strPassword

    Application.ScreenUpdating = True
    End Sub
    [/VBA]


    Greets,


    Michael

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub InsertRow()

    Application.ScreenUpdating = False

    ActiveSheet.Unprotect strPassword
    With Rows(ActiveCell.Row)
    .Insert
    .Offset(-2, 0).EntireRow.Copy
    .Offset(-1, 0).PasteSpecial xlPasteAll
    End With
    ActiveSheet.Protect strPassword

    Application.ScreenUpdating = True
    End Sub
    [/vba]

  4. #4
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location

    Smile

    Thanks ammx & xld


  5. #5
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location

    Another Question

    is it possible to right click the cell to make an insert/delete rows in a protected sheet instead of a button?
    (for ex:, when the user right click, the right click menu will have "Insert Between" or "Delete Selected")

    Thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use the modified code I gave you before and add this

    [vba]
    Option Explicit

    Private Const MENU_NAME As String = "Insert New Row"

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Application.CommandBars("Cell")
    On Error Resume Next
    .Controls(MENU_NAME).Delete
    On Error GoTo 0
    End With
    End Sub

    Private Sub Workbook_Open()
    With Application.CommandBars("Cell")
    On Error Resume Next
    .Controls(MENU_NAME).Delete
    On Error GoTo 0

    With .Controls.Add(Type:=msoControlButton, temporary:=True)
    .Caption = MENU_NAME
    .OnAction = "InsertRow"
    End With
    End With
    End Sub
    [/vba]

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code

  7. #7
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location

    Smile



    Thanks xld


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •