Если все, что вам нужно, это сжатие кода, вы можете удалить много лишнего. Value et c ...
Также If-ElseIf можно перекодировать, но они простые с использованием функции IIf
Обычно установка кода в операторах 'With' также помогает сократить код, а также повысить его эффективность, но здесь нет ничего, что могло бы гарантировать это
Например:
' If the document date is earlier than the date in the first line of the spreadsheet search for the date in the A column
ElseIf strSheetDate > strFileNameDate Then
Set rngSheetDate = WS1.Range("A:A").Find(strFileNameDate)
' If the date is found in A column then add the data to that row
If Not rngSheetDate Is Nothing Then 'when rng <> nothing means found something
If WS2.Cells(2, 2) <> "" Then rngSheetDate.Offset(0, 1) = rngSheetDate.Offset(0, 1) + WS2.Cells(2, 2)
If WS2.Cells(3, 2) <> "" Then rngSheetDate.Offset(0, 3) = rngSheetDate.Offset(0, 3) + WS2.Cells(3, 2)
Else
' If it is not found then look for dates either side of the document date and then insert a new line for that record
iRow2 = WS1.Cells(Rows.Count, 1).End(xlUp).Row
Set rngSheetDate2 = WS1.Range("A2:A" & iRow2)
For Each Cell In rngSheetDate2
If Cell > strFileNameDate And Cell.Offset(1, 0) < strFileNameDate Then
WS1.Range(Cell.Offset(1, 0), Cell.Offset(1, 4)).Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Cell.Offset(1, 0) = strFileNameDate
Cell.Offset(1, 1) = IIf(WS2.Cells(2, 2) <> "", WS2.Cells(2, 2), 0)
Cell.Offset(1, 3) = IIf(WS2.Cells(3, 2) <> "", WS2.Cells(3, 2), 0)
End If
Next
End If
End If