Sorry Ignore that last comment, I wrote it too quickly the real answer is this one:
H and I are the original columns that you wanted values output . It was only later that you added the requirement to clear E and F, The orginal solution defined an input array (Inarr) that picked up column D. so my solution rather than starting again, was extend the array that was orginally just column D to include columns E to F. I then cleared these in the array and wrote the array back, which wasn't orginally in the code. The outputs H and I weren't changed by this. So the way to clear columns H and I is clear the output array outarr
VBA Code:
inarr(i, 2) = "" ' this clears the value in column E
inarr(i, 3) = "" ' this clears the value in column F
outarr(i, 1) = ""
outarr(i, 2) = ""
What all of this demonstrates is that if you keep changing the requirements of a bit of code you end up with code that is messy and not consistent, because bits have been changed from their original design.
For the first iteration of the code Inarr was just used for Inputs and outarr was just used for outputs , now it is not quite as simple as that.
However it should work!!
I am trying to make sense of this code you provided...it works great by the way.
I am still a bit confused on a couple elements...I have commented on the code below where I am struggling to make sense of the code.
It is in the inarr and the outarr that i dont understand, could you explain that?
Sub test5() ' works perfect
With Worksheets("Labour")
lastrow = .Cells(Rows.Count, "D").End(xlUp).Row ' find the last row on labour sheet
datar = .Range(.Cells(1, 4), .Cells(lastrow, 16)) ' pick all the data in columns D to P from row 1 to the last row
End With
With Worksheets("TskSheet")
inarr = .Range(.Cells(13, 4), .Cells(27, 7)) ' pick up the values to look up
' WHAT IS THE 7?
outarr = .Range(.Cells(13, 8), .Cells(27, 9)) ' define the output array
' WHAT IS TH 9?
For i = 1 To UBound(inarr, 1) ' loop through each lookup value
If inarr(i, 1) = "" Then
inarr(i, 2) = "" ' this clears the value in column E
inarr(i, 3) = "" ' this clears the value in column F , delete this line if you don't need it
outarr(i, 1) = "" ' this addition clears H & I
outarr(i, 2) = "" ' this addition clears H & I
Else
For j = 1 To lastrow ' loop through each row of the Labour data to find a match
If inarr(i, 1) = datar(j, 1) Then ' check for a match
outarr(i, 1) = datar(j, 13) ' copy data to output when matched
outarr(i, 2) = inarr(i, 2) * inarr(i, 3) * inarr(i, 4) * outarr(i, 1) ' E * F * G * H
Exit For ' exit the inner loop because we have matched the look up
End If
Next j
End If
Next i
.Range(.Cells(13, 8), .Cells(27, 9)) = outarr ' write output array back to the workhseet
' WHAT IS TH 9?
.Range(.Cells(13, 4), .Cells(27, 7)) = inarr ' write column D to F back because we might have cleared some rows
' WHAT IS THE 7?
End With
End Sub