同事!将字符串从数组输出到新工作表时,代码(超出范围)崩溃(我什至不确定数组的形成是否一切正常):
Sub BKFindDeviations()
Dim Deviations() As Variant
Dim rng As Range
Dim Mnth1 As Date
Dim Mnth2 As Date
Dim MnthRowCounter As Long
Dim LoopCounter As Long
Dim r As Range
Dim k As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Mnth1 = "01.01.2020" 'Mnth1 = InputBox("Ââåäèòå ïåðâîå ÷èñëî ìåñÿöà, êîòîðûé áóäåò ñðàâíèâàòüñÿ ñ ïðîøëûìè ìåñÿöàì (ôîðìàò: dd.mm.yyyy")
Mnth2 = "01.02.2020" 'Mnth2 = InputBox("Ââåäèòå ïåðâîå ÷èñëî ìåñÿöà, êîòîðûé áóäåò ÿâëÿòüñÿ áàçîé äëÿ ñðàâíåíèÿ (ôîðìàò: dd.mm.yyyy")
For Each r In Range("a1", Range("a1").End(xlDown))
If r.Offset(0, 8).Value = Mnth1 Or r.Offset(0, 8).Value = Mnth2 Then
MnthRowCounter = MnthRowCounter + 1
ReDim Preserve Deviations(1 To 17, 1 To MnthRowCounter)
For LoopCounter = 1 To 17 'Range("a1", Range("a1").End(xlToRight))
Deviations(LoopCounter, MnthRowCounter) = r.Offset(0, LoopCounter - 1).Value
Next LoopCounter
End If
Next r
'Deviations = Range("a1", Range("a1").End(xlToRight).End(xlDown))
Worksheets.Add
Range(ActiveCell, ActiveCell.Offset(UBound(Deviations, 1) - 1, UBound(Deviations, 2) - 1)).Value = Deviations
Erase Deviations
Set rng = Range("a1", Range("a1").End(xlDown).End(xlToRight))
'×èñòèì ôîðìàò
With rng
.ClearFormats
End With
'Óñòàíàâëèâàåì ôîðìàò "Äàòà" â ñòîëáöå Äàò
For k = 1 To Cells(Rows.Count, 9).End(xlUp).Row
'If Cells(r, 9) Like "##.##.####" Then
'Cells(r, 9) = DateSerial(Right(Cells(r, 9), 4), Mid(Cells(r, 9), 4, 2), Left(Cells(r, 9), 2))
Cells(k, 9).NumberFormat = "dd.mm.yyyy;@"
'End If
Next k
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic End Sub
“出发”前算子所涉及的变量值:
更新了代码