Solved

vba code to Insert row

Posted on 2013-02-01
7
590 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
TT Text To Column Based On Criteria 3 18
TT Copy Formula 3 16
Name Rotation 11 29
Excel Worksheet or VBA Formula Question 4 15
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now