Попробуйте следующий код:
Option Explicit
Sub test()
Dim LastColumn2 As Long, LastRow1 As Long, LastRow2 As Long, i As Long, j As Long
Dim Code1 As String, Code2 As String
Dim Excist As Boolean
LastRow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow1
Code1 = Sheet1.Range("A" & i).Value
LastColumn2 = Sheet2.Cells(1, Sheet2.Columns.Count).End(xlToLeft).Column
Excist = True
For j = 1 To LastColumn2
Code2 = Sheet2.Cells(1, j).Value
If Code1 = Code2 Then
LastRow2 = Sheet2.Cells(Sheet2.Rows.Count, j).End(xlUp).Row
Sheet2.Cells(LastRow2 + 1, j).Value = Sheet1.Range("A" & i).Offset(0, 2).Value
Excist = True
Exit For
Else
Excist = False
End If
Next j
If Excist = False Then
If LastColumn2 = 1 And Sheet2.Range("A1").Value = "" Then
Sheet2.Cells(1, 1).Value = Sheet1.Range("A" & i).Value
Sheet2.Cells(2, 1).Value = Sheet1.Range("A" & i).Offset(0, 2).Value
Else
Sheet2.Cells(1, LastColumn2 + 1).Value = Sheet1.Range("A" & i).Value
Sheet2.Cells(2, LastColumn2 + 1).Value = Sheet1.Range("A" & i).Offset(0, 2).Value
End If
End If
Next i
End Sub
Данные отображаются на листе 1:
и экспорт на листе 2: