VBA Modyfing loop to create a new line on specific conditions

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Hello again good people of MrExcel thsi is the loop:
VBA Code:
For Each emptyCell In dRng
    If emptyCell.Value2 = CLng(FindDate) Then
        Dim colNum As Integer
        colNum = emptyCell.Column 'get the column number where the date was found
        For b = emptyCell.Row + 1 To emptyCell.Row + 32 'start the loop from the next row of the found date cell
            If Cells(b, colNum).Value = "" Then 'check if the cell is empty in the same column where the date was found
                Set emptyCell = Cells(b, colNum)
                ' Add jobtype and hours
                If Cells(emptyCell.Row, "B").Value = Me.employee.Value And Cells(emptyCell.Row, "C").Value = Me.JobType.Value Then
                emptyCell.Value = Me.HoursCount.Value
                emptyCellFound = True 'set the flag to indicate that an empty cell has been found
                Exit For
                Else
                    emptyCell.Value = Me.HoursCount.Value
                    Cells(emptyCell.Row, "C").Value = Me.JobType.Value
                    Cells(emptyCell.Row, "B").Value = Me.employee.Value
                    emptyCellFound = True 'set the flag to indicate that an empty cell has been found
                    Exit For 'exit the inner loop if an empty cell is found
                End If
            End If
        Next b
   
    End If

I want else statement of this code to apply to an empty cell in collumn but only if cell in B and C column of this row is also empty, not the first empty cell it encounters in this column.
so the loops has to find the first empty cell in a row where B and C are also empty for this else statement.
Visual reference:
I have this set of data created by the else statement of my given code:
1678372765792.png
I run the loop using the same JobType.values but with a newer date,
and I get an update to jobtype with new set of hours but for the next day:
1678372945290.png
Now the next day my employee did none of these jobtypes, he in fact did new jobtype "M",
So when i try to use this loop as it is I get:
1678373071330.png
as you can see it replaced the first jobtype with new jobtype cause it doesn't know that it should use different empty cell.
I want to get this instead:
1678373217280.png

I cant wrap my head around how to this loops to act this way I tried using nested loop to find new empty cell ina row where B and C is also empty but it only broke the code completely.
Maybe someone has an idea how make it happen?
Thank you in advance!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Mayeb an 'if' statement that lets 'Else' statement be executed only when cells in B and C column on the saem row are empty, else loop again to find new empty cell? But how to make this loop functional?
 
Upvote 0
Hi,
untested but try this addition to your code & see if it will do what you want

Rich (BB code):
For Each emptyCell In dRng
        If emptyCell.Value2 = CLng(FindDate) Then
            Dim colNum As Integer
            colNum = emptyCell.Column        'get the column number where the date was found
            For b = emptyCell.Row + 1 To emptyCell.Row + 32        'start the loop from the next row of the found date cell
                If Cells(b, colNum).Value = "" Then        'check if the cell is empty in the same column where the date was found
               
                'check if columns B  & C are both empty
               If Not CBool(Cells(b, 2) & Cells(b, 3) = "") Then
                   
                    Set emptyCell = Cells(b, colNum)
                    ' Add jobtype and hours
                    If Cells(emptyCell.Row, "B").Value = Me.employee.Value And Cells(emptyCell.Row, "C").Value = Me.JobType.Value Then
                        emptyCell.Value = Me.HoursCount.Value
                        emptyCellFound = True        'set the flag to indicate that an empty cell has been found
                        Exit For
                    Else
                        emptyCell.Value = Me.HoursCount.Value
                        Cells(emptyCell.Row, "C").Value = Me.JobType.Value
                        Cells(emptyCell.Row, "B").Value = Me.employee.Value
                        emptyCellFound = True        'set the flag to indicate that an empty cell has been found
                        Exit For        'exit the inner loop if an empty cell is found
                    End If
                   
                Else
                    'both cells empty - do stuff here
                   
                End If
               
            End If
        Next b
        
    End If
 
Upvote 0
Solution
Before applying this I'm trying to understand it.
How this block woudl work?
If b,2 and b, 3 is not empty then execute:
VBA Code:
                  If Cells(emptyCell.Row, "B").Value = Me.employee.Value And Cells(emptyCell.Row, "C").Value = Me.JobType.Value Then
                        emptyCell.Value = Me.HoursCount.Value
                        emptyCellFound = True        'set the flag to indicate that an empty cell has been found
                        Exit For
                    Else
                        emptyCell.Value = Me.HoursCount.Value
                        Cells(emptyCell.Row, "C").Value = Me.JobType.Value
                        Cells(emptyCell.Row, "B").Value = Me.employee.Value
                        emptyCellFound = True        'set the flag to indicate that an empty cell has been found
                        Exit For        'exit the inner loop if an empty cell is found
                    End If
?
 
Upvote 0
If B and or C are not empty, line should return False and execute your block otherwise, True for you to do whatever in the Else statement.

Dave
 
Upvote 0
If B and or C are not empty, line should return False and execute your block otherwise, True for you to do whatever in the Else statement.

Dave
Ok, this is the code I ended up with, hopefully it will work as expected it will show with more testing.
VBA Code:
'find date'
Sheets(sheetName).Activate
FindDate = CDate(Me.DateRange.Value)
Set dRng = Range("D7:J7,D67:J67,D127:J127,D187:J187,D247:J247,D307:J307,D367:J367,D427:J427,D487:J487,D547:J547,D607:J607")
Dim emptyCellFound As Boolean 'variable to track if an empty cell has been found
emptyCellFound = False

For Each emptyCell In dRng
    If emptyCell.Value2 = CLng(FindDate) Then
        Dim colNum As Integer
        colNum = emptyCell.Column 'get the column number where the date was found
        For b = emptyCell.Row + 1 To emptyCell.Row + 32 'start the loop from the next row of the found date cell
            If Cells(b, colNum).Value = "" Then 'check if the cell is empty in the same column where the date was found
                Set emptyCell = Cells(b, colNum)
               
                ' Add jobtype and hours
                If Not CBool(Cells(b, 2) & Cells(b, 3) = "") Then 'return fals and executes  below
                    If Cells(emptyCell.Row, "B").Value = Me.employee.Value And Cells(emptyCell.Row, "C").Value = Me.JobType.Value Then
                    emptyCell.Value = Me.HoursCount.Value
                    emptyCellFound = True 'set the flag to indicate that an empty cell has been found
                    Exit For
                    End If
                Else 'if cBool returns True executes below
                    emptyCell.Value = Me.HoursCount.Value
                    Cells(emptyCell.Row, "C").Value = Me.JobType.Value
                    Cells(emptyCell.Row, "B").Value = Me.employee.Value
                    emptyCellFound = True 'set the flag to indicate that an empty cell has been found
                    Exit For 'exit the inner loop if an empty cell is found
                End If
            End If
        Next b
   
    End If
   
    If emptyCellFound Then 'exit the outer loop if an empty cell has been found
        Exit For
    End If
Next emptyCell

If Not emptyCellFound Then 'check if an empty cell was found
    MsgBox "No empty cell available below " & dRng.Address
    Exit Sub
End If

Thank you for your assistance!
Have a great day!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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