Новый код для попытки: предполагается, что идентификатор сотрудника должен перезаписывать оставшуюся часть ввода, только сравнение, выполненное с существующими данными.
Sub TryThis()
Dim Data As Variant
'Loads your data into an array beginning at (1,1) and ending at (11,1)
Worksheets("Sheet1").Activate
Data = Range("B2", "B12")
'Selects worksheet 2 and puts in your data. I personally don't like the .activate, but for a simple program
'that this seems to be, it shouldn't hurt your performance.
Worksheets("Sheet2").Activate
'evaluates if it is the first entry by determing if cell is empty
If Range("A2") <> "" Then
'If it is not empty, sheet2 is put into an array (an array is overkill unless you have a lot of data)
Dim Comp As Variant
Comp = Range("A2", Range("A1").End(xlDown).End(xlToRight))
'looks at each employee ID already existing in sheet2
For i = 1 To UBound(Comp)
'If the employee Id exists, it will write over it here.
If Data(4, 1) = Comp(i, 4) Then
MsgBox "Employee ID Exists" & vbNewLine & "Employee Information Updated"
Dim CCount As Long
CCount = 1
Do Until CCount = 11
'used i + 1 because of your header on sheet2 and was too lazy to create a new variable
Cells(i + 1, CCount).Value = Data(CCount, 1)
CCount = CCount + 1
Loop
Worksheets("Sheet1").Activate
'Resets your input range
Range("B2:B17").Value = ""
'Since the information is written here, it will exit sub for next entry
Exit Sub
End If
Next i
End If
Dim RCount As Long
RCount = 2
Do Until Cells(RCount, 2) = ""
RCount = RCount + 1
Loop
CCount = 1
Do Until CCount = 11
Cells(RCount, CCount).Value = Data(CCount, 1)
CCount = CCount + 1
Loop
MsgBox "New Employee Id" & vbNewLine & "New Information Added"
Worksheets("Sheet1").Activate
Range("B2:B12").Value = ""
End Sub
Исходный код
Sub TryThis()
Dim Data As Variant
'Loads your data into an array beginning at (1,1) and ending at (11,1)
Worksheets("Sheet1").Activate
Data = Range("B2", "B18")
'Selects worksheet 2 and puts in your data. I personally don't like the .activate, but for a simple
'program that this seems to be, it shouldn't hurt your performance.
Worksheets("Sheet2").Activate
Dim RCount As Long
RCount = 2
Do Until Cells(RCount, 2) = ""
RCount = RCount + 1
Loop
Dim CCount As Long
CCount = 1
Do Until CCount = 17
Cells(RCount, CCount).Value = Data(CCount, 1)
CCount = CCount + 1
Loop
End Sub