Option Explicit ' Sub abc() Dim r As Range, p(1), i, j, n, t For Each r In ActiveSheet.UsedRange If InStr(r.Value, "工程量") Then For i = r.Column To 1 Step -1 If Cells(1, i).Value = 24 Then p(1) = i If Cells(1, i).Value = 25 Then p(0) = i: Exit For Next If p(0) = 0 Or p(1) = 0 Then MsgBox "无法定位日期": Exit Sub For i = 2 To Cells(Rows.Count, r.Column).End(xlUp).Row + 1 Step 2 ReDim a(1 To p(1) - p(0) + 1) If Cells(i, r.Column).Value > UBound(a) Then _ MsgBox "无法分配完工程量": Exit Sub For j = 1 To Cells(i, r.Column).Value: a(j) = 1: Next For j = 1 To UBound(a) n = Int(Rnd * (UBound(a) - j + 1)) + j t = a(j): a(j) = a(n): a(n) = t Cells(i + 1, j + p(0) - 1) = a(j) Next Next Exit Sub End If Next MsgBox "无法定位工程量" End Sub