Solved

Shift Rows Down, after copying from one sheet to another

Posted on 2016-10-04
7
46 Views
Last Modified: 2016-10-05
I am using the following script to move a row from one sheet to another.

Sub CopyRws()

Dim rng As Range
Dim intCols As Integer
Dim introw As Integer

Set rng = ActiveCell.CurrentRegion
intCols = rng.Columns.Count
introw = ActiveCell.Row

Range(Cells(introw, 1), Cells(introw, intCols)).Copy

Sheets("LogArchived").Select
Range("A7").PasteSpecial xlPasteAll

Sheets("TaskTracker").Select
Range(Cells(introw, 1), Cells(introw, intCols)).Delete xlShiftUp

Set rng = Nothing

End Sub

Open in new window


Issue is in Ln14, it overwrites what is there in the destination sheet (LogArchived) in A7.  How do I change this so it inserts and pushes what is on LogArchived sheet down at A7?  It would be great if I could select more than one row on the Target sheet (TaskTracker) in shift down however many rows were selected.  This way I could do more than one row at a time.   Please advise and thanks.
0
Comment
Question by:RWayneH
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41828387
You may try something like this....
Currently as per your code, only one row is being copied and if you somehow change your code to copy more than one rows, n would be evaluated accordingly (Line#12) and the copied rows would be inserted on the target sheet shifting the row7 down.
Sub CopyRws()

Dim rng As Range
Dim intCols As Long
Dim introw As Long
Dim n As Long
Set rng = ActiveCell.CurrentRegion
intCols = rng.Columns.Count
introw = ActiveCell.Row

Range(Cells(introw, 1), Cells(introw, intCols)).Copy
n = Range(Cells(introw, 1), Cells(introw, intCols)).Rows.Count
Sheets("LogArchived").Range("A7").Resize(n).Rows.Insert

Range(Cells(introw, 1), Cells(introw, intCols)).Delete xlShiftUp

Set rng = Nothing

End Sub

Open in new window

0
 

Author Comment

by:RWayneH
ID: 41828476
Ok that works nice, however if a user does select more than one row in the original selection it will not work.  I would like to leave as is with only one row at a time...  Is there a way to stop the user if more than one is selected?  Sort of like a "Sorry try again, selecting only one cell before moving?  I want to prevent more than one from moving for now.  I need to do some other things to the line after it moves.
0
 
LVL 28

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41828548
How can a user select more than one row as per the existing code?
See the below line which copies a predefined range in the code..
Range(Cells(introw, 1), Cells(introw, intCols)).Copy

Open in new window


So as you can see that there is nothing in the code which interacts with the user.
If you want to copy the range selected by the user rather than a predefined range, try the below code.
Ask the use to select the range to be copied and then run the code.

Sub CopyRws()
Dim n As Long
n = Selection.Rows.Count
Selection.Copy
Sheets("LogArchived").Range("A7").Resize(n).Rows.Insert
Selection.Delete xlShiftUp
End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RWayneH
ID: 41828666
I actual have a need for both options..  one that does multiple rows and another that does just one at a time.  How do I make sure that only one cell is selected before the procedure runs?  All working good, so far.  Thanks.
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41828679
You may count the no. of rows in the selection before continuing like below....
Sub CopyRws()
Dim n As Long
n = Selection.Rows.Count
If n > 1 Then
   MsgBox "The Selected Range contains more than one Row." & vbNewLine & vbNewLine & _
            "Please select range with one row and try again...", vbExclamation, "Invalid Selection!"
   Exit Sub
End If
Selection.Copy
Sheets("LogArchived").Range("A7").Resize(n).Rows.Insert
Selection.Delete xlShiftUp
End Sub

Open in new window

0
 

Author Closing Comment

by:RWayneH
ID: 41829938
Thanks for the help with this, works awesome.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41829980
You're welcome!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

896 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

15 Experts available now in Live!

Get 1:1 Help Now