【以下回答由 GPT 生成】
Sub FindMaxValue()
Dim maxVal As Double
Dim maxRow As Long
Dim sumVal As Double
Dim currentVal As Double
Dim i As Long
maxVal = 0
maxRow = 0
sumVal = 0
' 获取C列的最后一行
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
' 遍历C列
For i = 1 To lastRow
' 获取当前行C列单元格的数值
currentVal = Cells(i, 3).Value
' 如果当前数值加上之前的求和结果不超过1000
If currentVal + sumVal <= 1000 Then
' 更新求和结果
sumVal = sumVal + currentVal
' 更新最大值和对应的行号
If sumVal > maxVal Then
maxVal = sumVal
maxRow = i
End If
Else
' 如果超过1000,则重置求和结果为当前数值
sumVal = currentVal
End If
Next i
' 将最大值和对应行号分别写入D1和E1单元格
Range("D1").Value = maxVal
Range("E1").Value = Cells(maxRow, 1).Value
End Sub
运行这段VBA宏代码可以实现你的需求。这段代码会遍历C列中的每个数值,并计算这个数值以及其后续相邻单元格(除C1外)的和,判断是否不超过1000。如果和超过1000,则会将下一个数值重新作为求和的起点。在遍历过程中,会记录最大的和及其对应的行号。最后,将最大和写入D1单元格,对应的A列数值写入E1单元格。