Solved

vba code to Insert row

Posted on 2013-02-01
7
635 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 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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