Solved

vba code to Insert row

Posted on 2013-02-01
7
621 Views
Last Modified: 2013-02-02
Would someone be able to provide me with some VBA code that will insert a row and copy the cells above. I would like the code to ask at what position the user wants to insert the row. i.e. 'Enter the row No where new row is to be inserted'

Thank you
0
Comment
Question by:Jagwarman
  • 4
  • 3
7 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
ID: 38843025
Something like the following should do it:

Sub Macro1()
    theRow = InputBox("Enter Row Number to insert")

    Rows(theRow - 1 & ":" & theRow - 1).Copy
    Rows(theRow & ":" & theRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Application.CutCopyMode = False
End Sub

Open in new window

0
 

Author Closing Comment

by:Jagwarman
ID: 38843065
Brilliant thanks 'The_Barman'
0
 

Author Comment

by:Jagwarman
ID: 38843148
The_Barman

Would you be able to add one more bit of code for me please.

When the Row is inserted, Go To the Cell in column 'B' and clear contents from that cell.

To clarify, when the row is inserted and details are copied down from the row above, I need the contents in B?  [only] to be cleared.

Many thanks
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 24

Expert Comment

by:Steve
ID: 38843333
Sub Macro1()
    theRow = InputBox("Enter Row Number to insert")

    Rows(theRow - 1 & ":" & theRow - 1).Copy
    Rows(theRow & ":" & theRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Application.CutCopyMode = False
    Cells(theRow,2).ClearContents
    Cells(theRow,2).Select
End Sub
0
 

Author Comment

by:Jagwarman
ID: 38843370
Perfect Thanks.

I have also posted this......

I am using the below code which contains a password. Is there any code that would prompt a password change every 30 days?

Sub Approval()
    resp = InputBox("Enter Password")
    Select Case resp
    Case "silver1":
   
    Insert
   
        Case Else: MsgBox "Invalid Password"
    End Select
   
End Sub

would you be able to look at it?

Regards
0
 

Author Comment

by:Jagwarman
ID: 38844387
Would you be able to assist me with this.

I keep getting

Prevent "to prevent possible loss of data, excel cannot shift nonblank cells off the worksheet. Select another location in which to insert new cells or delete data from the end of your worksheet."

I am using 2010. When I was using 2003 this never happened.

If I select End/Down Delete and then save the workbook it lets me insert the row.

As I am using your code this is very annoying.

Is there a way to stop this?

Thanks
0
 
LVL 24

Expert Comment

by:Steve
ID: 38846265
Would you be able to post a copy of the workbook, I can then see what the issues may be.

As for the password Q will happily take a look.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question