运行代码时没有任何事情发生

嗨,大家好,这是我的代码

但是,当我运行它没有发生,显然它运行在rest模式(debugging)。 我能做什么 ? 干杯

Option Explicit Sub Button2_Click() Dim i&, j&, n& 'as Long Dim Ws As Worksheet Set Ws = ActiveSheet n = Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row For i = 1 To n Select Case .Cells(i, "A").Value2 Case "2015 Xor 2011": .Cells(i, 2).Value2 = "blue" Case "2001 Xor 2003": .Cells(i, 2).Value2 = "green" Case "2014 Xor 2006": .Cells(i, 2).Value2 = "red" End Select j = j + 1 Next i End With End Sub 

在A列中find的年份中,应该填写B列的颜色名称

我不确定j var在哪里发挥作用; 我可以想象,这是涉及到一些代码被认为是不重要的这个问题已被删除。

 Sub Button2_Click() Dim i As Long, j As Long, n As Long Dim Ws As Worksheet Set Ws = ActiveSheet With Ws n = Ws.Cells(Rows.Count, "A").End(xlUp).Row For i = 1 To n Select Case .Cells(i, "A").Value2 Case 2015, 2011 .Cells(i, 2).Value2 = "blue" Case 2001, 2003 .Cells(i, 2).Value2 = "green" Case 2014, 2006 .Cells(i, 2).Value2 = "red" End Select j = j + 1 Next i End With End Sub 

Select Case语句可以使用逗号分隔的列表处理Xor比较。

尝试下面的代码。 你说大约有20K行,所以所有的计算都是在arrays上进行的,以获得更好的性能。

 Sub Button2_Click() Dim years As Variant Dim colors() As Variant Dim i As Long Dim ws As Excel.Worksheet Dim lastRow As Long '------------------------------------------------------ Set ws = Excel.ActiveSheet 'Read the values from column A to array - it will make the calculations faster. With ws lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row years = .Range(.Cells(1, 1), .Cells(lastRow, 1)) End With 'Resize [colors] array to adjust its size to the size of [years] array. ReDim colors(LBound(years, 1) To UBound(years, 1), 1 To 1) For i = LBound(years, 1) To UBound(years, 1) Select Case years(i, 1) Case 2015, 2011: colors(i, 1) = "blue" Case 2001, 2003: colors(i, 1) = "green" Case 2014, 2006: colors(i, 1) = "red" End Select Next i 'Paste the result array back into the worksheet ws.Cells(1, 2).Resize(UBound(years) - LBound(years) + 1, 1) = colors End Sub