Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 66
  • Last Modified:

Shift Rows Down, after copying from one sheet to another

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
RWayneH
Asked:
RWayneH
  • 4
  • 3
2 Solutions
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
RWayneHAuthor Commented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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!

 
RWayneHAuthor Commented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
RWayneHAuthor Commented:
Thanks for the help with this, works awesome.
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now