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: 6

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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