【问题标题】:Selecting Cells when looping through Columns in VBA循环遍历VBA中的列时选择单元格
【发布时间】:2018-08-23 16:51:31
【问题描述】:

我有一个用于格式化多页 Excel 文档的宏。每张纸上都有一个数据表,我正在尝试根据标题行(第 1 行)的内容对列进行颜色编码。

我想选择列中的第二个单元格,直到最后一行数据,并设置背景颜色。我目前有这样的东西:

For Each ws In ActiveWorkbook.Worksheets
    With ws
        .Activate

        ' Convert to table
        If .UsedRange.ListObject Is Nothing Then
            .ListObjects.Add SourceType:=xlSrcRange,
                             Source:=.UsedRange, 
                             xllistobjecthasHeaders:=xlYes, 
                             TableStyleName:="Table Style 1"
        End If

        ' Additional Formatting
        ...

        ' Adjust columns
        For Each col In .UsedRange.Columns
            Header = col.Cells(1, 1)

            ' Additional formatting that works
            ...

            Dim col_color As Variant

            ' Color Code
            If Header = "A" Then
                col_color = xlThemeColorAccent6 ' Orange
            ElseIf Header ="B" Then
                col_color = xlThemeColorLight2 ' Blue
            ElseIf Header = "C" Then
                col_color = xlThemeColorAccent4 ' Purple
            ElseIf Header ="D" Then
                col_color = xlThemeColorAccent3 ' Green
            ElseIf Header = "E" Then
                col_color = xlThemeColorAccent2 ' Red
            End If

            Range(col.Cells(2, 1)).Select ' ERROR HERE
            Range(Selection, Selection.End(xlDown)).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = col_color
                .TintAndShade = 0.8
                .PatternTintAndShade = 0
            End With


        Next col

我在 Range(col.Cells(2, 1)).Select 上收到以下错误,“对象 '_Global' 的方法 'Range' 失败”。

我的问题是如何在循环的当前列迭代中正确选择第二个单元格?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我想选择列中的第二个单元格,直到最后一行数据,并设置背景颜色。

    无需创建Selection 来设置背景颜色。正确声明一个范围变量并改用它:

    Dim formatRange as Range
    Set formatRange = Range(col.Cells(2, 1), col.Cells(2, 1).End(xlDown) )
    
    With formatRange.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = col_color
        .TintAndShade = 0.8
        .PatternTintAndShade = 0
    End With
    

    您的错误发生是因为您只向Range 方法传递了一个参数,该方法是一个单元格,其默认属性是Value,因此除非col.Cells(2,1) 包含一个有效范围作为其值address 字符串,这将始终失败。您可以通过将两个参数传递给Range 来避免它单个 Select:

    Range(col.Cells(2, 1), col.Cells(2, 1).End(xlDown)).Select
    With Selection.Interior
        ...
    

    或者:

     col.Cells(2, 1).Select
     Range(Selection, Selection.End(xlDown)).Select
     With Selection.Interior
        ...    
    

    甚至:

     col(2,1).Select
     Range(Selection, Selection.End(xlDown)).Select
     With Selection.Interior
        ...     
    

    但你不应该做那些事情:

    它几乎是普遍的better to avoid Activate/Select

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多