根据行VBA中的单元格字体颜色隐藏列 [英] hide column based on cell font color in a row vba

查看:163
本文介绍了根据行VBA中的单元格字体颜色隐藏列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据第一行中的单元格字体颜色隐藏列.如果第一行中的单元格具有黑色作为字体颜色,我想在这里隐藏列.现在,我不想定义一个范围,因为我的数据每周都在变化,所以我本周可以有10列,下周可以有20列,而且我想保持代码动态.

I am trying to hide columns based on cell font color in first row. Here I want to hide columns if cells in first row has black as font color. Now, I dont want to define a range since my data varies each week so I can have 10 columns this week and 20 next week, and I would like to keep my codes dynamic.

我的vba代码似乎没有问题(通过按 F8 ),但没有任何反应.我还在下面发布了示例以及期望的结果.

It looks like my vba codes went through (by pressing F8) without any issues but nothing happens. I also posted a sample below along with the desire result.

 Sub test()
 Dim i As Range

  For Each i In Rows(1)
       If i.Font.Color = RGB(0, 0, 0) Then
         i.EntireColumn.Hidden = True
            Else
            i.EntireColumn.Hidden = False

        End If
Next i

 End Sub

推荐答案

您需要引用该范围中的单元格.而且您不需要If语句.试试:

You need to refer to the cells in the range. And you don't need the If statement. Try:

Sub foo()
    Dim c As Range
    For Each c In ActiveSheet.UsedRange.Rows(1).Cells
        c.EntireColumn.Hidden = c.Font.Color = 0
    Next c
End Sub

这篇关于根据行VBA中的单元格字体颜色隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆