Timesheet add a row based on inputbox entry

cbye

New Member
Joined
Feb 2, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a timesheet that I need to be able to add a row if the employee worked a double shift. The VBA code that I found seems to be what I need but when I input the date I get "Input not found in range" message. Im not sure what Im missing.

Sub Date_Finder()

Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet '<-- Update
Dim xInput As Date, Found As Range

xInput = Application.InputBox("Enter Date [mm/dd/yyyy]", Type:=1)

If IsDate(xInput) Then
Set Found = ws.Range("B:B").Find(xInput)

If Found Is Nothing Then
MsgBox "Input not found in range"
Else
Found.Offset(1).EntireRow.Insert (xlShiftDown)
Found.Offset(1).EntireRow.Insert (xlShiftDown)
End If
Else
MsgBox "Invalid Entry. Ending sub" & vbNewLine & "Entry: " & xInput, vbCritical
End If

End Sub



Cell Formulas
RangeFormula
E9:E38E9=IF(I9>$F$6,$F$6,I9)
F9:F38F9=MOD((D9-C9),1)*24
B9B9=F4
B10:B38B10=B9+1
E40E40=SUM(E9:E39)
F40,F42F40=SUM(E40:E40)
E42E42=E41*E40
F44F44=F40
F47F47=F42
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C9:C30,C33:C39Cellcontains a blank value textNO
C9:C30,C33:C39Cell Value>"24:00:00"textNO
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What's the Cell format of the data in Column B. I would think that it should be Date.
 
Upvote 0
Hi

using Range.Find to locate dates can be a problem but try this update to your code & see if does what you want

VBA Code:
Sub Date_Finder()
    
    Dim ws              As Worksheet
    Dim xInput         As Variant
    Dim Found        As Range
    
    Set ws = ThisWorkbook.ActiveSheet        '<-- Update
    
    Do
        xInput = InputBox("Enter Date [mm/dd/yyyy]")
        'cancel pressed
        If StrPtr(xInput) = 0 Then Exit Sub
    Loop Until xInput Like "##/##/####" And IsDate(xInput)
    
    xInput = DateValue(xInput)
    
    Set Found = ws.Columns(2).Find(xInput, LookIn:=xlFormulas, LookAt:=xlWhole)
    
    If Not Found Is Nothing Then
    
        Found.Offset(1).Resize(2).EntireRow.Insert (xlShiftDown)
        
    Else
    
        MsgBox xInput & Chr(10) & "Input Not found in range", 48, "Not Found"
        
    End If
    
End Sub

Dave
 
Upvote 0
What's the Cell format of the data in Column B. I would think that it should be Date.
Hey Skybot, I took a screenshot of the format. Also in my immediate window if I issue a ?isdate(activecell) it comes back as True. And ?range("B14").value it comes back as 7/6/2023

1690736499519.png
 
Upvote 0
Hi

using Range.Find to locate dates can be a problem but try this update to your code & see if does what you want

VBA Code:
Sub Date_Finder()
   
    Dim ws              As Worksheet
    Dim xInput         As Variant
    Dim Found        As Range
   
    Set ws = ThisWorkbook.ActiveSheet        '<-- Update
   
    Do
        xInput = InputBox("Enter Date [mm/dd/yyyy]")
        'cancel pressed
        If StrPtr(xInput) = 0 Then Exit Sub
    Loop Until xInput Like "##/##/####" And IsDate(xInput)
   
    xInput = DateValue(xInput)
   
    Set Found = ws.Columns(2).Find(xInput, LookIn:=xlFormulas, LookAt:=xlWhole)
   
    If Not Found Is Nothing Then
   
        Found.Offset(1).Resize(2).EntireRow.Insert (xlShiftDown)
       
    Else
   
        MsgBox xInput & Chr(10) & "Input Not found in range", 48, "Not Found"
       
    End If
   
End Sub

Dave
Hi Dave, thanks for replying. I tried your code but once I input the date in the window and click ok, it presents the window again and doesn't add a row.
 
Upvote 0
Hi Dave, thanks for replying. I tried your code but once I input the date in the window and click ok, it presents the window again and doesn't add a row.

As I stated, Range.Find when searching for dates can be a problem

PROVIDING the dates in your range are all in a consistent format you can try this update & see if resolves the issue

VBA Code:
Sub Date_Finder()
  
    Dim ws              As Worksheet
    Dim xInput         As Variant
    Dim Found        As Range
  
    Const DateFormat As String = "mm/dd/yyyy" '< change as required
  
    Set ws = ThisWorkbook.ActiveSheet        '<-- Update
  
    Do
        xInput = InputBox("Enter Date [" & DateFormat & "]")
        'cancel pressed
        If StrPtr(xInput) = 0 Then Exit Sub
    Loop Until xInput Like "##/##/####" And IsDate(xInput)
  
    xInput = Format(DateValue(xInput), DateFormat)
  
    Set Found = ws.Columns(2).Find(xInput, LookIn:=xlValues, LookAt:=xlWhole)
  
    If Not Found Is Nothing Then
  
        Found.Offset(1).Resize(2).EntireRow.Insert (xlShiftDown)
      
    Else
  
        MsgBox xInput & Chr(10) & "Input Not found in range", 48, "Not Found"
      
    End If
  
End Sub

Dave
 
Upvote 0
Solution
Hey Skybot, I took a screenshot of the format. Also in my immediate window if I issue a ?isdate(activecell) it comes back as True. And ?range("B14").value it comes back as 7/6/2023

View attachment 96244
Try this custom format.
ddd,d mmm
Since you're looking for a full Date in the VBA code you may nee to add Year data?
ddd,m/d/yyyy
 
Upvote 0
Thanks again for both of your inputs. I think Im understanding that with Dave's code, the date formats in green and my cell range format need to match 1 for 1. When I set my range format to dd/mm/yyyy it works! It was adding 2 rows though. I changed the .Resize to 1 and it now adds 1 row. Thanks.

To take this one step further, I would like for the date cell of the new row to auto populate with the date that was entered initially. Would that the xInput? Or would it just be easier to copy the B cell just above the inserted row?







Sub Date_Finder()

Dim ws As Worksheet
Dim xInput As Variant
Dim Found As Range

Const DateFormat As String = "dd/mm/yyyy" '< change as required

Set ws = ThisWorkbook.ActiveSheet '<-- Update

Do
xInput = InputBox("Enter Date [" & DateFormat & "]")
'cancel pressed
If StrPtr(xInput) = 0 Then Exit Sub
Loop Until xInput Like "##/##/####" And IsDate(xInput)

xInput = Format(DateValue(xInput), DateFormat)

Set Found = ws.Columns(2).Find(xInput, LookIn:=xlValues, LookAt:=xlWhole)

If Not Found Is Nothing Then

Found.Offset(1).Resize(2).EntireRow.Insert (xlShiftDown)

Else

MsgBox xInput & Chr(10) & "Input Not found in range", 48, "Not Found"

End If

End Sub
 
Upvote 0
Try adding this
VBA Code:
Found.Offset(1).Resize(1).EntireRow.Insert (xlShiftDown)
Found.Offset(1).Value = xInput     '<--------------added line
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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