Solved

Shift Rows Down, after copying from one sheet to another

Posted on 2016-10-04
7
42 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 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

13 Experts available now in Live!

Get 1:1 Help Now