I had a bit of code which was working:
WbShortage = "name.xlsm"
For l = TotalDefectsPerPieces + 1 To TheLastRow
For i = StartDate To EndDate
Date = Cells(DateRow, i).Value
[...]
With Workbooks(WbShortage).Worksheets("Report")[...]
Then I added this to check which file is opened (there are two options):
WbShortage1 = "name.xlsm"
WbShortage2 = "name2.xlsm"
If IsWorkBookOpen(WbShortage1) = True Then
WbShortage = WbShortage1
Else
WbShortage = WbShortage2
End If
For l = TotalDefectsPerPieces + 1 To TheLastRow
For i = StartDate To EndDate
Date = Cells(DateRow, i).Value
[...]
With Workbooks(WbShortage).Worksheets("Report")[...]
And now in the line Date = Cells(DateRow, i).Value
there is type mismatch error. When I delete If statement everything is fine. I just don't get it, this line in in no way dependent on WbShortage as it corresponds to another Wb.
Of course I have this function:
Function IsWorkBookOpen(sWB)
On Error GoTo NotOpen
Workbooks(sWB).Activate
IsWorkBookOpen = True
Exit Function
NotOpen:
IsWorkBookOpen = False
End Function