Solved

vba code to Insert row

Posted on 2013-02-01
7
599 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

910 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

22 Experts available now in Live!

Get 1:1 Help Now