Goal: Transfer Sheet Test1
2nd Column to Sheet TestS
2nd Column.
Where:
For each CPU in Test1. If CPU name in Sheet Test1 match CPU name in sheet TestS, copy 2nd column of that CPU in Test1 and paste in 2nd column of that CPU in TestS.
Else, create new row with the unfound CPU name and 2nd column after the last CPU of sheet TestS
.
Final look in TestS
looks like:
This is my current working code where variables needs to be declared (string, integer etc):
Sub automateCpu()
Dim noofComps As Long
noOfComps = Worksheets("Test1").Range("A1").CurrentRegion.Rows.Count-1
Dim x As Long
x = 0
Dim d As Integer
d = 0
Sheets("Test1").Select
Range("A1").Select
Do While x > noOfComps
Selection.Offset(1, 0).Select
Dim curComputer As String
curComputer = Selection.Value
Dim test1Row As Integer
d = d + 1
test1Row = ActiveCell.Row
Sheets("TestS").Select
Range("A1").Select
Dim y As Integer
y = 0
Dim z As Integer
z = 0
Dim LastRowSummary As integer
LastRowSummary = Worksheets("TestS").Range("A1").CurrentRegion.Rows.Count - 1
Do While y < LastRowSummary
Selection.Offset(1, 0).Select
If Selection.Value = curComputer Then
z = 1
Dim SummaryRow As Integer
SummaryRow = ActiveCell.Row
Sheets("Test1").Select
Range("B" & test1Row).Select
Dim curLogValue As Integer
curLogValue = Selection.Value
Sheets("TestS").Select
Range("B" & SummaryRow).Select
Selection.Value = curLogValue
Sheets("Test1").Select
Range("A" & test1Row).Select
Exit Do
End If
y = y + 1
Loop
If z = 0 Then
d = d + 1
Selection.Offset(1, 0).Select
Selection.Value = curComputer
SummaryRow = ActiveCell.Row
Sheets("Test1").Select
Range("B" & test1Row).Select
Dim curLogValue As Integer
curLogValue = Selection.Value
Sheets("TestS").Select
Range("B" & SummaryRow).Select
Selection.Value = curLogValue
Sheets("Test1").Select
Range("A" & test1Row).Select
End If
z = 0
d = d + 1
x = x + 1
Loop
End Sub
Pseudo Code:
1) Loop CPU in Test1
2) Find CPU in TestS by looping each computer
3) If CPU Matches for both Test1 & TestS paste 2nd Column
4) Else, create new row for new computer and 2nd column.
My question is how can I make this code more efficient? Or any alternate method that can reduce the amount of codes etc.