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

x
?
Solved

Shift Rows Down, after copying from one sheet to another

Posted on 2016-10-04
7
Medium Priority
?
64 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
[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 32

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 32

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 2000 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
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!

 

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 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 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 32

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

670 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