Cannot create a new time-in entry

genesix

New Member
Joined
Jul 9, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi guys could you please correct my code because it is only working in preventing a duplicate time-in entry in my Sheet1 worksheet.

My objective is user Peter Pan should have a daily time-in entry in the worksheet using my excel VB userform

Problem - once the code check for existing entry, it can no longer add a new time-in entry for user Peter Pan.


Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Application.ScreenUpdating = False
With Sheets("Sheet1")

If WorksheetFunction.CountIf(.Columns(3), ComboBox1) > 0 Then
MsgBox " You are Already Timed-in", vbCritical, "Employee Time-in"
Else
' Find emtpy row
lRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
'Add data to worksheet
sh.Range("A" & last_Row + 1).Value = "=Row()-1"
sh.Range("B" & last_Row + 1).Value = Me.TextBox1.Value
sh.Range("C" & last_Row + 1).Value = Me.ComboBox1.Value
sh.Range("D" & last_Row + 1).Value = Me.ComboBox2.Value
sh.Range("E" & last_Row + 1).Value = Date
sh.Range("F" & last_Row + 1).Value = Time

MsgBox "SUCCESSFULLY LOGGED IN", Title:="VISUAL TIMESHEET"

End If

End With
'-----------------------Clear data after saving
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.TextBox1.Value = ""

Call Refresh_Data

End Sub
 

Attachments

  • form.png
    form.png
    9.2 KB · Views: 7
  • worksheet.png
    worksheet.png
    36.2 KB · Views: 5

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the Board!

Well, your calculation for last row in named "lRow", but then you use "last_Row" in the range references!
So the value "last_Row" is always 0, meaning you are always pasting to row 1 (since you are adding 1 to it).

By the way, if you turn on "Option Explicit", VBA would have caught this typo for you!
See here: Option Explicit in Excel VBA.
 
Upvote 0
Hi guys could you please correct my code because it is only working in preventing a duplicate time-in entry in my Sheet1 worksheet.

My objective is user Peter Pan should have a daily time-in entry in the worksheet using my excel VB userform

Problem - once the code check for existing entry, it can no longer add a new time-in entry for user Peter Pan.


Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Application.ScreenUpdating = False
With Sheets("Sheet1")

If WorksheetFunction.CountIf(.Columns(3), ComboBox1) > 0 Then
MsgBox " You are Already Timed-in", vbCritical, "Employee Time-in"
Else
' Find emtpy row
lRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
'Add data to worksheet
sh.Range("A" & last_Row + 1).Value = "=Row()-1"
sh.Range("B" & last_Row + 1).Value = Me.TextBox1.Value
sh.Range("C" & last_Row + 1).Value = Me.ComboBox1.Value
sh.Range("D" & last_Row + 1).Value = Me.ComboBox2.Value
sh.Range("E" & last_Row + 1).Value = Date
sh.Range("F" & last_Row + 1).Value = Time

MsgBox "SUCCESSFULLY LOGGED IN", Title:="VISUAL TIMESHEET"

End If

End With
'-----------------------Clear data after saving
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.TextBox1.Value = ""

Call Refresh_Data

End Sub
Welcome to the Board!

Well, your calculation for last row in named "lRow", but then you use "last_Row" in the range references!
So the value "last_Row" is always 0, meaning you are always pasting to row 1 (since you are adding 1 to it).

By the way, if you turn on "Option Explicit", VBA would have caught this typo for you!
See here: Option Explicit in Excel VBA.
what do you mean, kindly explain further please?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top