L
Legacy 458280
Guest
Hi,
I'm having a bit of trouble using a 'find' method within a 'for each' loop. The code works perfectly, except where it finds repeated values, it only executes on the first instance.
Quick overview:
This is a project scheduling document. This piece of code (which runs via the press of a button) is supposed to delete the currently selected project (in the 'Projects' sheet) as well as deleting any reference to that project from the 'Board' and 'Resource Assignment' sheets. The board is the primary interface, whilst the 'Projects' and 'Resource Assignment' sheets serve as primitive databases.
Here is the code:
I have attached screenshots for reference (names have been removed). As you can see on the 'Board' sheet, some employees have multiple instances of the same project against their name (i.e. employee on row 11 working on Project 1234 on 28th Jan, then has a few days off before returning to Project 1234 again from 3rd-4th Feb). In this case, one of the blocks will be removed from the board (and also removed from the resource assignment sheet), but the other one won't. I assume this is down to the 'For Each' loop only picking up on the FIRST result from the find method contained within the 'if' statement, though I haven't had any luck with resolving it.
Any suggestions would be greatly appreciated.
Thanks,
Luke
I'm having a bit of trouble using a 'find' method within a 'for each' loop. The code works perfectly, except where it finds repeated values, it only executes on the first instance.
Quick overview:
This is a project scheduling document. This piece of code (which runs via the press of a button) is supposed to delete the currently selected project (in the 'Projects' sheet) as well as deleting any reference to that project from the 'Board' and 'Resource Assignment' sheets. The board is the primary interface, whilst the 'Projects' and 'Resource Assignment' sheets serve as primitive databases.
Here is the code:
VBA Code:
Private Sub CommandButton3_Click()
Static selectRow As Range
Dim projectNum As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Call UnProtectAllSheets
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Selection.Rows.Count > 1 Then
MsgBox "Please only select one row at a time.", vbOKOnly, "Multiple Rows Selected"
Call ProtectAllSheets
Exit Sub
End If
If Selection.Row < 3 Then
MsgBox "Please select a project.", vbOKOnly, "Invalid Selection"
Call ProtectAllSheets
Exit Sub
End If
Set selectRow = Selection.EntireRow
projectNum = selectRow.Cells(1, 1).Value
If projectNum = "" Then
MsgBox "Please select a valid project.", vbOKOnly, "Invalid Selection"
Call ProtectAllSheets
Exit Sub
End If
If projectNum = "HOL" Then
MsgBox "The 'Holiday' row cannot be deleted. This is used to assign holidays to site employees.", vbOKOnly, "Invalid Selection"
Call ProtectAllSheets
Exit Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim assignmentSheet As Worksheet
Dim boardSheet As Worksheet
Dim assignmentLR As Long
Dim assignmentRange As Range
Dim assignmentSearchRow As Range
Dim resourceAssignmentEmployee As String
Dim resourceAssignmentStart As Date
Dim resourceAssignmentEnd As Date
Dim boardEmployeeRow As Long
Dim boardLR As Long
Dim boardEmployeeRange As Range
Dim boardStartColumn As Long
Dim boardEndColumn As Long
Dim find_start As String
Dim find_end As String
Dim boardLC As Long
Dim boardDateRange As Range
Dim boardAssignmentCellStart As Range
Dim boardAssignmentCellEnd As Range
Dim boardAssignmentRange As Range
Dim ColLet As String
Set assignmentSheet = ThisWorkbook.Sheets("Resource Assignment")
Set boardSheet = ThisWorkbook.Sheets("Board")
assignmentLR = assignmentSheet.Cells(Rows.Count, "B").End(xlUp).Row
Set assignmentRange = assignmentSheet.Range("A" & 3 & ":D" & assignmentLR)
boardLR = boardSheet.Cells(Rows.Count, "B").End(xlUp).Row
Set boardEmployeeRange = boardSheet.Range("B" & 4 & ":B" & boardLR)
boardLC = boardSheet.Cells(3, Columns.Count).End(xlToLeft).Column
ColLet = Split(Cells(1, boardLC).Address, "$")(1)
Set boardDateRange = boardSheet.Range("C" & 3 & ":" & ColLet & 3)
For Each assignmentSearchRow In assignmentRange.Rows
If assignmentSearchRow.Cells(1, 2).Value = projectNum Then
resourceAssignmentEmployee = assignmentSearchRow.Cells(1, 1).Value
resourceAssignmentStart = assignmentSearchRow.Cells(1, 3).Value
resourceAssignmentEnd = assignmentSearchRow.Cells(1, 4).Value
find_start = Format(resourceAssignmentStart, "Short Date")
find_end = Format(resourceAssignmentEnd, "Short Date")
boardEmployeeRow = boardEmployeeRange.Find(What:=resourceAssignmentEmployee, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows).Row
boardStartColumn = boardDateRange.Find(What:=CDate(find_start), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns).Column
boardEndColumn = boardDateRange.Find(What:=CDate(find_end), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns).Column
Set boardAssignmentCellStart = boardSheet.Cells(boardEmployeeRow, boardStartColumn)
Set boardAssignmentCellEnd = boardSheet.Cells(boardEmployeeRow, boardEndColumn)
Set boardAssignmentRange = boardSheet.Range(boardAssignmentCellStart, boardAssignmentCellEnd)
boardAssignmentRange.Interior.Color = xlNone
boardAssignmentRange.ClearContents
boardAssignmentRange.ClearComments
boardAssignmentRange.ClearNotes
boardAssignmentRange.HorizontalAlignment = xlCenter
assignmentSearchRow.EntireRow.Delete
End If
Next assignmentSearchRow
selectRow.Delete
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Call ProtectAllSheets
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
I have attached screenshots for reference (names have been removed). As you can see on the 'Board' sheet, some employees have multiple instances of the same project against their name (i.e. employee on row 11 working on Project 1234 on 28th Jan, then has a few days off before returning to Project 1234 again from 3rd-4th Feb). In this case, one of the blocks will be removed from the board (and also removed from the resource assignment sheet), but the other one won't. I assume this is down to the 'For Each' loop only picking up on the FIRST result from the find method contained within the 'if' statement, though I haven't had any luck with resolving it.
Any suggestions would be greatly appreciated.
Thanks,
Luke