输出循环VBA中的循环事件

我目前正在用颜色编码的事件来分析一个时间表,这个事件在一行中出现,时间在列上扩展(在单元格中只有颜色没有文本,是的,它是愚蠢的,但是它不在我的控制之内)。 我目前能够将一个事件发生的结果打印到另一个工作表中,但我无法确定如何在不同的单元格中打印周期性事件的date(它目前只打印上一次发生事件)。 我目前的代码是:

For i = 2 To 93 If Cells(7, i).Interior.Color = "8421631" And Cells(7, i - 1).Interior.Color = "16777215" Then startDay = Cells(3, i).Value startMonth = Cells(1, i).Value name = Cells(7, i).Value Worksheets("Sheet1").Activate ActiveWorkbook.Worksheets("Sheet1").Cells(2, 1) = startDay + startMonth End If If Cells(7, i).Interior.Color = "8421631" And Cells(7, i + 1).Interior.Color = "16777215" Then endDay = Cells(3, i).Value endMonth = Cells(1, i).Value ActiveWorkbook.Worksheets("Sheet1").Cells(2, 2) = endDay + endMonth End If Next i 

我所有的variables都以stringformsinput。 我尝试了几种不同的方法,但都没有成功。 我觉得我需要在IF语句中增加一个循环,但我不确定如何做到最好。 总的来说,代码实现了它的目的(以前用于输出的消息框来确认它是否按照它的操作)。 这只是整个代码的一小部分,但是这个答案我可以在其他地方应用。

为了我能从你的描述中得到的东西,你可以试试这个代码:

 Option Explicit Sub main() Dim i As Long For i = 2 To 93 If Cells(7, i).Interior.Color = "8421631" Then If Cells(7, i - 1).Interior.Color = "16777215" Or Cells(7, i + 1).Interior.Color = "16777215" Then WriteDate Cells(3, i).Value, Cells(1, i).Value End If End If Next i End Sub Sub WriteDate(day As String, month As String) With ActiveWorkbook.Worksheets("Sheet1") .Cells(2, .Columns.Count).End(xlToLeft).Offset(, 1) = day & "/" & month End With End Sub