Hello, I am using VBA userform in MS Excel to insert data for database purpose. When I tested it for the first time, it was running okay. But after I saved it and used it again to insert the real data, it happens to repeat/duplicate the data to multiple times (up to 5 rows) in the spreadsheet.
Here are the codes I am using to export the data from userform to spreadsheet.
Private Sub OKButton_Click()
Dim emptyRow As Long
'Make Sheet3 Active
If UnplannedCategoryComboBox.Value = "Anti-Surge Valve System (AVS)" Then Sheets(3).Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Export Data to worksheet
Cells(emptyRow, 1).Value = UnplannedDateTextBox.Value
Cells(emptyRow, 2).Value = UnplannedMonthComboBox.Value
If UnplannedYearComboBox.Value = "2002" Then Cells(emptyRow, 3).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2003" Then Cells(emptyRow, 4).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2004" Then Cells(emptyRow, 5).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2005" Then Cells(emptyRow, 6).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2006" Then Cells(emptyRow, 7).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2007" Then Cells(emptyRow, 8).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2008" Then Cells(emptyRow, 9).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2009" Then Cells(emptyRow, 10).Value = UnplannedDurationTextBox.Value
Cells(emptyRow, 11).Value = UnplannedCauseFailureTextBox.Value
'Make Sheet4 Active
If UnplannedCategoryComboBox.Value = "Centrifugal Gas Compressor (GC)" Then Sheets(4).Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Export Data to worksheet
Cells(emptyRow, 1).Value = UnplannedDateTextBox.Value
Cells(emptyRow, 2).Value = UnplannedMonthComboBox.Value
If UnplannedYearComboBox.Value = "2002" Then Cells(emptyRow, 3).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2003" Then Cells(emptyRow, 4).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2004" Then Cells(emptyRow, 5).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2005" Then Cells(emptyRow, 6).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2006" Then Cells(emptyRow, 7).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2007" Then Cells(emptyRow, 8).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2008" Then Cells(emptyRow, 9).Value = UnplannedDurationTextBox.Value
If UnplannedYearComboBox.Value = "2009" Then Cells(emptyRow, 10).Value = UnplannedDurationTextBox.Value
Cells(emptyRow, 11).Value = UnplannedCauseFailureTextBox.Value
End Sub
So, the question is, how do I prevent the data from duplicating itself in the spreadsheet? Where did I do wrong in the coding? Please help me.
Thank you in advance.