为什么在rng.Clear之后不进行格式化?

时间:2019-05-11 16:12:19

标签: excel vba

我正在做一些小事,以减少工作量。 我决定,由于使用VB进行频繁的数据刷新将是恒定格式化和清除单元格的最佳分辨率。我已经有5年没有接触VB了,我不明白为什么这不起作用。

该代码应该可以工作,我将其检查为不同的按钮,但是在进行某些复制和粘贴之后,条件格式不起作用。

Set rng = Range("B1:B15")

    rng.Clear

    'Formating Data with color to Value
    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 = 8109667
        .TintAndShade = 0
    End With

代码应清空单元格B1至B15并对其进行格式化,以根据值更改颜色。它只是颜色格式化的唯一部分,我不想放入所有24行代码。

1 个答案:

答案 0 :(得分:1)

  1. 您的问题尚不清楚,但是ClearClearContents之间有细微差别

    • Range.Clear清除Range对象中的所有公式格式
    • Range.CearContents清除单元格的内容(值),但保留格式不变

因此,请确保您使用的是正确的,即使有必要也可以使用两者,avoid Select altogether也是个好主意。就您而言,Selection仅在用户在工作表中选择特定的Range时有效。

  1. 似乎您可能想将格式应用于rng对象

    rng.FormatConditions.AddColorScale ColorScaleType:=3 'and so on..