Solved

Shift Rows Down, after copying from one sheet to another

Posted on 2016-10-04
7
49 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 29

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 29

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 29

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 29

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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