循环通过excel列

问题描述 投票:1回答:1

我有NFL统计数据,我正在登录表格以创建条件格式化的热图。

从E列开始到P列结束是我的数据,但我需要它从a2开始并转到该列的最后一行,然后对每一列执行相同的操作,以便条件不重叠并创建一个大的热图。我只想为每个单独的列提供热图,以便我可以分析这种方式。 (并非所有列都包含数字,并且它们已经扩散,因此有条件的格式化不应该随便拾取文本列)

如何在不明确引用列的情况下循环列?我对一个职位的状态不会包含与另一个职位相同数量的列。

我需要这个尽可能的动态。此外,任何人都可以帮我清理条件格式化文件吗?我只是复制了宏记录的代码,因为我不知道如何自己编译。

我在想这样的事情:

Dim Overall_Stats As Workbook
Dim RB_stats As Worksheet
Set RB_stats = Overall_Stats.Sheets(RB)

LastRow = Range("A" & Rows.Count).End(xlUp).Row

with RB_stats
     .Range("A2:A" & LastRow)
     .FormatConditions.AddColorScale ColorScaleType:=3
     .FormatConditions(.FormatConditions.Count).SetFirstPriority
     .FormatConditions(1).ColorScaleCriteria(1).Type = _
      xlConditionValueLowestValue

With .FormatConditions(1).ColorScaleCriteria     (1).FormatColor
    .Color = 8109667
    .TintAndShade = 0
End With
.FormatConditions(1).ColorScaleCriteria(2).Type = _
    xlConditionValuePercentile
.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With .FormatConditions(1).ColorScaleCriteria(2).FormatColor
    .Color = 8711167
    .TintAndShade = 0
End With
.FormatConditions(1).ColorScaleCriteria(3).Type = _
    xlConditionValueHighestValue
With .FormatConditions(1).ColorScaleCriteria(3).FormatColor
    .Color = 7039480
    .TintAndShade = 0
End With

End With

For i = 1 to 100
    Columns(i).Select
next I
excel vba excel-vba
1个回答
1
投票

我无法帮助您进行条件格式化,但您可以通过逐步运行代码并检查每个步骤的效果来轻松配置它(尽管不是很快)。

要在动态变化的工作表中查找有趣的列,需要做三件事。

  1. 找到最右边的列 Dim lastcol as long lastcol=RB_stats.usedrange.columns.count

注意:这种方法很简单,不太可靠,但是当你在最后一列数据中没有任何东西时可以使用它。完全找到最右边的列是另一门科学,请参阅here

  1. 确定列是否包含数字:假设第2行包含数据,您可以在单元格中测试类型的值: If Typename(Cells(2, col))="Double" Then ... this is a column of numbers, do formatting

Typename为数字返回Double,其他值为StringDateEmpty

  1. 在所选列上应用格式 For col=1 to lastcol If Typename(Cells(2, col))="Double" Then ' this is a column of numbers, select range for formatting LastRow = Cells(Rows.Count, col).End(xlUp).Row with RB_stats.Range(Cells(2, col), Cells(LastRow, Col) ... do formatting here end with End If Next
© www.soinside.com 2019 - 2024. All rights reserved.