vba - 多个单元格选择,而不是范围

标签 vba excel loops cells

我想循环遍历每一行并对选定的多个单元格执行一些操作,例如K3,N3,Q3,T3,W3,Z3 接下来 K4,N4,Q4...

我做错了什么?

Sub Colors_test()
    For counter = 3 To 110
    Range("K" & counter, "N" & counter, "Q" & counter, "T" & _
         counter, "W" & counter, "Z" & counter).Select

    Selection.FormatConditions.AddColorScale ColorScaleType:=3
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
        xlConditionValueLowestValue
    With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
        .Color = 7039480
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
        xlConditionValuePercentile
    Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
    With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
        .Color = 8711167
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
        xlConditionValueHighestValue
    With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
        .Color = 8109667
        .TintAndShade = 0
    End With

    Next counter
    MsgBox "Ok All " & counter
End Sub

最佳答案

无需循环。您可以使用以下代码。

Sub Colors_test()


    With Range("K3:K110,N3:N110,Q3:Q110,T3:T110,W3:W110,Z3:Z110")
        .Select
      // your code here

    End With

End Sub

关于vba - 多个单元格选择,而不是范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15581368/

相关文章:

html - 在网站上使用多种形式输入 key

python - 在 python for 循环中一次运行 3 个变量。

Postgresql 函数卡在 for 循环中

excel - 将备用单元格行剪切/复制/粘贴到下一列并在之后删除空行

Excel:显示从开始日期和结束日期生成的月份名称集合?

mysql - 错误类型 : ADODB. Recordset (0x800A0E79) 打开对象时不允许操作

python - 如何在Python中测试单元格坐标以更改数据?

java - 使用if语句添加while循环java

excel - 在 VBA (Excel) 中获取时区信息

sql-server - SSIS - 强制 Excel 连接