Finding value of textbox in specified range or ranges

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Hi, I have an issue with this code that i cannot resolve.
Although the code is big I am mainly concerned about this line:
Set dRng = Sheets(sheetName).Range("D7:J7").Find(What:=Me.DateRange.Value, LookIn:=xlValues)
In this case range D7:J7 corresponds to dates incremented by one so e.g. D7 = 23.02.2023, D8 = 24.02.2023 and so on
let's say Me.DateRange.Value = 23.02.2023
This line of code finds the value in this range without a problem and executes the rest of the code "almost" perfectly, but thats another topic.
As you can tell this is a weekly set of dates and ends on Sunday. When the week ends I create a new "page" with new set of data moving the previous set of excel lines with previosu week range of dates below
so now Me.DateRange.Value = 23.02.2023 can now eb found in D67, and week afetr that it will be D127 so on and so forth.
I cannot set up the .Find to fidn this date on sheet no matter what.
It's worth noting that I ahve some merged cells on that sheet, so when I use D7:J it gets lost.
Tried many approaches, but I just can't seem to find this value on sheet no matter what.
here's the code:
also, here's the screen shot of the sheet where "older" values have been already moved below.
1677249267704.png

VBA Code:
Private Sub Submit_Click()
Dim emp As String
Dim cell As Range
Dim i As Integer
'check if Holiday or Sick is checked'
If Me.Holiday.Value = True And Me.Sick.Value = True Then
    MsgBox "Please select only one checkbox", vbCritical, "Error"
    Exit Sub
End If

'check if Holiday is checked'
If Me.Holiday.Value = True Then
    If Len(Me.DateRange.Value) = 0 Then
        MsgBox "Please enter a Date Range", vbCritical, "Error"
        Exit Sub
    End If
    emp = Me.employee.Value
    Set cell = Sheets("MKP_" & emp).Range("A10:A40").Find(Me.DateRange.Value)
    If Not cell Is Nothing Then
        cell.Offset(0, 2).Value = "UW"
    Else
        MsgBox "Update MKP with current dates", vbCritical, "Error"
        Exit Sub
    End If
End If


'check if Sick is checked'
If Me.Sick.Value = True Then
    If Len(Me.DateRange.Value) = 0 Then
        MsgBox "Please enter a Date Range", vbCritical, "Error"
        Exit Sub
    End If
    emp = Me.employee.Value

    Set cell = Sheets("MKP_" & emp).Range("A10:A40").Find(Me.DateRange.Value)
    If Not cell Is Nothing Then
        cell.Offset(0, 2).Value = "CH"
    Else
        MsgBox "Update MKP with current dates", vbCritical, "Error"
        Exit Sub
    End If
End If


'check if either EndedProjectNo combobox or ActiveProjectNo combobox has a value, if yes, determine the sheet to update
    If Not IsEmpty(EndedProjectNo.Value) Or Not IsEmpty(ActiveProjectNo.Value) Then
        Dim ans As String
        Dim sheetName As String
        Dim dRng As Range
 
               
        If Len(Me.ActiveProjectNo.Value) > 5 Then
            Me.EndedProjectNo.Value = ""
            ans = Me.ActiveProjectNo.Value
            sheetName = "Godziny" + Left(ans, 5)
        ElseIf Len(Me.EndedProjectNo.Value) > 5 Then
            ActiveProjectNo.Value = ""
            ans = Me.EndedProjectNo.Value
            sheetName = "Godziny" + Left(ans, 5)
        End If
    End If

Set dRng = Sheets(sheetName).Range("D7:J7").Find(What:=Me.DateRange.Value, LookIn:=xlValues)

If Not dRng Is Nothing Then
    Dim emptyCell As Range
    Dim a As Integer
    
    For a = dRng.Row + 1 To 39
        If Cells(a, dRng.Column).Value = "" Then
            Set emptyCell = Cells(a, dRng.Column)
            Exit For
        End If
    Next a
    If Not emptyCell Is Nothing Then
        emptyCell = Me.HoursCount.Value
        Cells(emptyCell.Row, "C").Value = Me.JobType.Value
        Cells(emptyCell.Row, "B").Value = Me.employee.Value
    Else
        MsgBox "No empty cell available below " & dRng.Address
    End If


    ' Find next empty cell for JobType2 and HoursCount2
    For a = dRng.Row + 1 To 39
        If Cells(a, dRng.Column).Value = "" Then
            Set emptyCell = Cells(a, dRng.Column)
            Exit For
        End If
    Next a
    If Not emptyCell Is Nothing Then
        If Not IsEmpty(Me.JobType2.Value) And Not IsEmpty(Me.HoursCount2.Value) Then
            emptyCell = Me.HoursCount2.Value
            Cells(emptyCell.Row, "C").Value = Me.JobType2.Value
            Cells(emptyCell.Row, "B").Value = Me.employee.Value
        End If
    Else
        MsgBox "No empty cell available below " & dRng.Address
    End If

    ' Find next empty cell for JobType3 and HoursCount3
    For a = dRng.Row + 1 To 39
        If Cells(a, dRng.Column).Value = "" Then
            Set emptyCell = Cells(a, dRng.Column)
            Exit For
        End If
    Next a
    If Not emptyCell Is Nothing Then
        If Not IsEmpty(Me.JobType3.Value) And Not IsEmpty(Me.HoursCount3.Value) Then
            emptyCell = Me.HoursCount3.Value
            Cells(emptyCell.Row, "C").Value = Me.JobType3.Value
            Cells(emptyCell.Row, "B").Value = Me.employee.Value
        End If
    Else
        MsgBox "No empty cell available below " & dRng.Address
    End If

    ' Find next empty cell for JobType4 and HoursCount4
    For a = dRng.Row + 1 To 39
        If Cells(a, dRng.Column).Value = "" Then
            Set emptyCell = Cells(a, dRng.Column)
            Exit For
        End If
    Next a
    If Not emptyCell Is Nothing Then
        If Not IsEmpty(Me.JobType4.Value) And Not IsEmpty(Me.HoursCount4.Value) Then
            emptyCell = Me.HoursCount4.Value
            Cells(emptyCell.Row, "C").Value = Me.JobType4.Value
            Cells(emptyCell.Row, "B").Value = Me.employee.Value
        End If
    Else
        MsgBox "No empty cell available below " & dRng.Address
    End If

    ' Find next empty cell for JobType5 and HoursCount5
    For a = dRng.Row + 1 To 39
        If Cells(a, dRng.Column).Value = "" Then
            Set emptyCell = Cells(a, dRng.Column)
            Exit For
        End If
    Next a
    If Not emptyCell Is Nothing Then
        If Not IsEmpty(Me.JobType5.Value) And Not IsEmpty(Me.HoursCount5.Value) Then
            emptyCell = Me.HoursCount5.Value
            Cells(emptyCell.Row, "C").Value = Me.JobType5.Value
            Cells(emptyCell.Row, "B").Value = Me.employee.Value
        End If
    Else
        MsgBox "No empty cell available below " & dRng.Address
    End If

    ' Find next empty cell for JobType5 and HoursCount5
    For a = dRng.Row + 1 To 39
        If Cells(a, dRng.Column).Value = "" Then
            Set emptyCell = Cells(a, dRng.Column)
            Exit For
        End If
    Next a
    If Not emptyCell Is Nothing Then
        If Not IsEmpty(Me.JobType6.Value) And Not IsEmpty(Me.HoursCount6.Value) Then
            emptyCell = Me.HoursCount6.Value
            Cells(emptyCell.Row, "C").Value = Me.JobType6.Value
            Cells(emptyCell.Row, "B").Value = Me.employee.Value
        End If
    Else
        MsgBox "No empty cell available below " & dRng.Address
    End If
Else
    MsgBox "Value not found"
End If
End Sub
I would appreciate some help in this matter
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi
Replace That line with
VBA Code:
With Sheets(sheetName)
Set dRng = .Range("D7:J" & .Cells(Rows.Count, 8).End(xlUp).Row).Find(Me.DateRange.Value, , , 1)
End With
 
Upvote 0
Hi
Replace That line with
VBA Code:
With Sheets(sheetName)
Set dRng = .Range("D7:J" & .Cells(Rows.Count, 8).End(xlUp).Row).Find(Me.DateRange.Value, , , 1)
End With
Still not finding the value.
I have to try different approach. Maybe I'll try with arrays.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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