View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Sean Sean is offline
external usenet poster
 
Posts: 14
Default Adding right click menu item to insert a blank row

Thanks Greg,

I appreciate the info, will try your suggestion

Sean
"Greg Wilson" wrote in message
...
Sean,

The problem is more than what you describe. If you copy a range of rows
then
it will insert the same number of rows as you copied instead of just one.
All
the inserted rows will also be populated as you described. I don't know
how
to control this.

The first of the below two macros will add a temporary button ("Insert
Row")
to the right click popup menu and set its OnAction property to the
InsertRow
macro. I made it temporary so that it will not persist after Excel is
closed.
You can use the Workbook_Open event to add it automatically for
appropriate
workbooks. You can optionally remove the Temporay:=True statement to make
it
permanent and can delete it programmatically if you want.

The second macro works by:
1. Passing the clipboard contents to a variable
2. Clearing the clipboard
3. Inserting a row at the position of the active cell
4. Refilling the clipboard with the saved contents that were passed to
the
variable
5. Then clicking the Paste button will paste the copied contents to the
active cell even though Cut/Copy mode is no longer on ("marching ants"
border
is no longer active).

Note that for the second macro to work you will have to set a reference to
the Microsoft Forms 2.0 Object Library through the VBE's toolbar: Tools
References. I've never had a need for this myself and so have no
experience
with it. Just wrote it now (minimal testing). Seems OK.

Regards,
Greg

Sub AddInsertBtn()
Dim btn As CommandBarButton
With Application.CommandBars("Cell")
Set btn = .Controls.Add(Befo=6, Temporary:=True)
btn.Caption = "Insert Row"
btn.OnAction = "InsertRow"
End With
End Sub

Private Sub InsertRow()
Dim DataObj As DataObject
Set DataObj = New DataObject
Dim clipdata As String
DataObj.GetFromClipboard
clipdata = DataObj.GetText
Application.CutCopyMode = False
ActiveCell.EntireRow.Insert
DataObj.SetText clipdata
DataObj.PutInClipboard
End Sub