L
Legacy 458280
Guest
Hi,
This is my first post. I think I'm posting this in the correct place, but apologies in advance if not. I've been struggling with this line of code for the past few days now, having seemingly searched through Google in its entirety, to no avail...
I'm having trouble setting a row as a variable in VBA. I'm using a "For Each" expression with a nested "If, Then" statement to find the first row that meets the given criteria.
Here is the code I'm using:
The problem is that I get a Type Mismatch error on "If val1 = Cells(searchRow, "A").Value And val2 >= Cells(searchRow, "C").Value And val3 <= Cells(searchRow, "D").Value Then" when running the code. I can't figure out what's causing this, though, as the respective columns are formatted with the correct data types (text, date and date).
The 'val1', 'val2' and 'val3' variables work fine (when running the code, hovering over them shows the correct values) and hovering over "searchRow.Row" appears to give the correct row number, but it won't allow it to be set as a variable, due to the Type Mismatch above.
As a result, I haven't been able to test any of the code below this point without resolving the Type Mismatch error.
Any help would be greatly appreciated. If you need any more information, please do let me know.
Many thanks,
Luke
This is my first post. I think I'm posting this in the correct place, but apologies in advance if not. I've been struggling with this line of code for the past few days now, having seemingly searched through Google in its entirety, to no avail...
I'm having trouble setting a row as a variable in VBA. I'm using a "For Each" expression with a nested "If, Then" statement to find the first row that meets the given criteria.
Here is the code I'm using:
VBA Code:
Sub DeleteAssignment()
Static selectRow As Long
Dim selectEmployee As String
Dim selectDate As Date
selectRow = ActiveCell.row
selectEmployee = Cells(selectRow, 2).Value
selectDate = Cells(2, ActiveCell.Column).Value
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim searchRng As Range
Dim searchRow As Range
Dim assignRow As Range
Dim Lastrow As Long
Dim val1 As String
Dim val2 As Date
Dim val3 As Date
Application.ScreenUpdating = False
val1 = selectEmployee
val2 = selectDate
val3 = selectDate
Lastrow = ThisWorkbook.Sheets("Resource Assignment").Cells(Rows.Count, "A").End(xlUp).row
Set searchRng = ThisWorkbook.Sheets("Resource Assignment").Range("A" & 3 & ":D" & Lastrow)
For Each searchRow In searchRng.Rows
If val1 = Cells(searchRow, "A").Value And val2 >= Cells(searchRow, "C").Value And val3 <= Cells(searchRow, "D").Value Then
assignRow = searchRow.row
End If
Next searchRow
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim startDate As Date
Dim endDate As Date
Dim startColumn As Long
Dim endColumn As Long
Dim startCell As Long
Dim endCell As Long
Dim assignmentRange As Range
startDate = assignRow.Cells(1, 3).Value
endDate = assignRow.Cells(1, 4).Value
startColumn = ThisWorkbook.Sheets("Board").Find(startDate, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
endColumn = ThisWorkbook.Sheets("Board").Find(endDate, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
startCell = ThisWorkbook.Sheets("Board").Cells(selectRow, startColumn)
endCell = ThisWorkbook.Sheets("Board").Cells(selectRow, endColumn)
Set assignmentRange = ThisWorkbook.Sheets("Board").Range("startCell:endCell")
assignmentRange.Clear
assignRow.EntireRow.Delete
ThisWorkbook.Sheets("Board").Activate
Application.ScreenUpdating = True
End Sub
The problem is that I get a Type Mismatch error on "If val1 = Cells(searchRow, "A").Value And val2 >= Cells(searchRow, "C").Value And val3 <= Cells(searchRow, "D").Value Then" when running the code. I can't figure out what's causing this, though, as the respective columns are formatted with the correct data types (text, date and date).
The 'val1', 'val2' and 'val3' variables work fine (when running the code, hovering over them shows the correct values) and hovering over "searchRow.Row" appears to give the correct row number, but it won't allow it to be set as a variable, due to the Type Mismatch above.
As a result, I haven't been able to test any of the code below this point without resolving the Type Mismatch error.
Any help would be greatly appreciated. If you need any more information, please do let me know.
Many thanks,
Luke