Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vba code to Insert row

Posted on 2013-02-01
7
Medium Priority
?
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

704 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