1

I wanted to have a code wherein there will be an error or prompt in the userform if the cell in the sheet- wherein the data will be transferred, already has content. As of now, the code I'm using doesn't show any prompt but it succeeds in not updating the cell if a data is to be transferred to it again.

Private Sub CancelButton_Click()
Unload Me
End Sub

Private Sub ClearButton_Click()
Call UserForm_Initialize
End Sub

Private Sub OKButton_Click()
Dim emptyRow As Long

'Make Sheet1 active
Sheet1.Activate

'Determine emptyRow
Dim rFound As Range: Set rFound = Range("B:B").Find(BarcodeTextBox.Value, , , xlWhole)
If rFound Is Nothing Then
    emptyRow = Range("B" & Rows.Count).End(xlUp).Row + 1
Else
    emptyRow = rFound.Row
End If

'Transfer information

If TimeOptionButton1.Value = True Then
    Cells(emptyRow, 5).Value = "Yes"
End If
If TimeOptionButton2.Value = True Then
    Cells(emptyRow, 7).Value = "Yes"
End If
If BreakOptionButton1.Value = True Then
    Cells(emptyRow, 9).Value = "Yes"
End If
If BreakOptionButton2.Value = True Then
    Cells(emptyRow, 11).Value = "Yes"
End If
If BreakOptionButton3.Value = True Then
    Cells(emptyRow, 14).Value = "Yes"
End If
If BreakOptionButton4.Value = True Then
    Cells(emptyRow, 16).Value = "Yes"
End If

Cells(emptyRow, 2).Value = BarcodeTextBox.Value

Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub

Private Sub UserForm_Initialize()
'Set Time In as default
TimeOptionButton1.Value = True

'Empty BarcodeTextBox
BarcodeTextBox.Value = ""

Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub

Thank you in advance!

Time Stamp Userform

Output Sheet

Community
  • 1
  • 1

1 Answers1

0

Concerning your code, I think that you may add something like this:

If TimeOptionButton1.Value = True Then
    if len(cells(emptyRow,5)) = 0 then
        MsgBox "Write Error Message here"
    else
        Cells(emptyRow, 5).Value = "Yes"        
    end if
End If

For each yes. You may consider building a separate function/sub later to avoid repetition.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Hi, thank you for your comment :) However, the code does not compile. If TimeOptionButton1.Value = True Then [if len(cells(emptyRow,5) = 0 then] - there is something wrong with this part. These texts became red after encoding it. MsgBox "Write Error Message here" else Cells(emptyRow, 5).Value = "Yes" end if End If Sorry I am really no expert. – Chenille Masangya Dec 21 '16 at 00:56
  • 1
    I got it! There's just a missing close parenthesis. Thank you so much! – Chenille Masangya Dec 21 '16 at 01:00
  • Parenthesis added in the answer :) – Vityata Dec 21 '16 at 08:09
  • If you liked the answer, feel free to mark is at accepted and to give it +1, when you can:) – Vityata Dec 21 '16 at 08:32