问题
I have an excel sheet that have a lot of data with various info (i.e...mailing info, best contact person, legal name, phone number, insurance requirements....) New contacts are added all the time and then are filtered in alphabetical order on the "Prime Data" sheet.
When I insert a row, on "Prime Data" to add a new contact, the VBA code that I use will copy the data to "Prime - Tracking" sheet and move the cells down to account for the inserted line. The problem is that it only moves the cells down in that column and do not insert a new row across all columns.
The "Prime - Tracking" have about 20 columns per row to account for a checklist of items to collect. When the inserted line is brought over from the "Prime Data" it does not act like I inserted a row across all columns but adds a line to just column A. This then makes the tracking of all items be from the vendor below/above the inserted contact.
I am currently using the below code to do this.
Private Sub CommandButton1_Click()
Dim SrcRng As Range
With Worksheets("Prime Data")
Set SrcRng = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With
Worksheets("Prime - Tracking").Range("A2").Resize(SrcRng.Rows.Count, 1).Value = SrcRng.Value
End Sub
回答1:
A simple copy paste for a row from sheet to sheet is:
Dim i as long, lr as long
i = 'source data row
With Worksheets("Prime Data")
.Rows(i).Copy
End with
With Worksheets("Prime - Tracking")
lr = .cells(.rows.count,1).end(xlup).row
.Rows(lr+1).Paste 'or .PasteSpecial xlValues
End With
If you wanted to insert, you could:
Dim i as long
i = 'source data row
With Worksheets("Prime Data")
.Rows(i).Copy
End with
With Worksheets("Prime - Tracking")
.Rows(2).Insert xlShiftDown
End With
Third way, if copy/paste is taking too long:
Dim i as long
i = 'source data row
With Worksheets("Prime - Tracking")
.Rows(2).Insert xlShiftDown
.Rows(2).value = Worksheets("Prime Data").Rows(i).value
End With
来源:https://stackoverflow.com/questions/51659804/i-want-to-copy-all-rows-from-column-a-on-worksheet-prime-data-and-paste-into-c