Faintkitara
Board Regular
- Joined
- Jun 23, 2016
- Messages
- 59
Hi! I came up with code that filters a set of data and then copies and pastes certain columns(Customer, Project Name, vs) into another worksheet (in columns N, O, and P)
Is there a more efficient way to write this code? Im having to copy/paste the same code three times in one sub.
Someone once told me that if you have to copy and paste something in code, that probably means you can write it better. Thanks for any help
Is there a more efficient way to write this code? Im having to copy/paste the same code three times in one sub.
Someone once told me that if you have to copy and paste something in code, that probably means you can write it better. Thanks for any help
Code:
Sub CopyPasteData1()Const Row As Integer = 4
Const Cust As String = "Customer"
Const Pro As String = "Project Name"
Const vs As String = "vs"
Dim LC As Integer, J As Integer
Dim lr As Integer
LC = Cells(Row, Columns.Count).End(xlToLeft).Column
For J = LC To 1 Step -1
If (Cells(Row, J) = Cust) Then
lr = Cells(Rows.Count, J).End(3).Row
Range(Cells(Row, J), Cells(lr, J)).Copy
Worksheets("Backlog").Range(Cells(Row, J), Cells(lr, J)).Copy _
Destination:=Worksheets("Pivot ").Range("N4:N10000")
End If
Next
LC = Cells(Row, Columns.Count).End(xlToLeft).Column
For J = LC To 1 Step -1
If (Cells(Row, J) = Pro) Then
lr = Cells(Rows.Count, J).End(3).Row
Range(Cells(Row, J), Cells(lr, J)).Copy
Worksheets("Backlog").Range(Cells(Row, J), Cells(lr, J)).Copy _
Destination:=Worksheets("Pivot").Range("O4:O10000")
End If
Next
LC = Cells(Row, Columns.Count).End(xlToLeft).Column
For J = LC To 1 Step -1
If (Cells(Row, J) = vs) Then
lr = Cells(Rows.Count, J).End(3).Row
Range(Cells(Row, J), Cells(lr, J)).Copy
Worksheets("Backlog").Range(Cells(Row, J), Cells(lr, J)).Copy _
Destination:=Worksheets("Pivot").Range("P4:P10000")
End If
Next
On Error Resume Next
Worksheets("Backlog").ShowAllData
On Error GoTo 0
End Sub