我可以以列标题词开始范围吗?

时间:2019-03-26 20:28:22

标签: excel vba

我有几个基本上彼此堆叠的表,所有列都在A列中。因此,第一个表从Steps(列标题)开始,然后在其下移1-n(无论如何),并且旁边有一个按钮在该表的正下方添加行。然后跳过几行,下面还有另一个类似的表格,该表格的列标题为STEPS1,并在下方列出了步骤编号,并带有另一个添加行的按钮。我有将根据步骤#为表中的某些单元格着色的代码,尽管当我添加一堆行时,我为着色单元格定义的范围会扩展并开始为我不想着色的单元格着色。因此,我可以定义一个以列标题开头的范围,而不是For all c in Range("A23:A32")

以下是一些代码:

Dim C As Range
For Each C In Range("A22:A38")
            If C = 1 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B12").Interior.Color 'whatever colour you need
            ElseIf C = 2 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B13").Interior.Color
            ElseIf C = 3 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B14").Interior.Color
            ElseIf C = 4 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B15").Interior.Color
            ElseIf C = 5 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B16").Interior.Color
            ElseIf C = 6 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B17").Interior.Color
            ElseIf C = 7 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B18").Interior.Color
            ElseIf C = 8 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B19").Interior.Color
            ElseIf C = 9 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B20").Interior.Color
            ElseIf C = 10 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B21").Interior.Color
            ElseIf C = 11 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B22").Interior.Color
            ElseIf C = 12 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B23").Interior.Color
            ElseIf C = 13 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B24").Interior.Color
            ElseIf C = 14 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B25").Interior.Color
            ElseIf C = 15 Then
                Union(Cells(C.row, 2), Cells(C.row, 5), Cells(C.row, 8), Cells(C.row, 11), Cells(C.row, 14)).Interior.Color = Range("B26").Interior.Color
            End If
        Next C
        Loop

我的问题是,如果我向每个表添加一堆行,则我设置的范围现在不能说满整个表。我想要一个可以找到诸如STEP1或STEP2之类的列标题的区域,直到出现空白行

1 个答案:

答案 0 :(得分:0)

请阅读并查看它是否有用。您可以使用A列中标题的名称来调用此子例程。

Sub ScanAndColor(tblName As String)

    Dim row As Integer
    row = 1

    'scan down until you find tblName, this is like "STEPS" or "STEPS1" or whatever
    Do While Cells(row, 1).Text <> tblName

        'just read the next row
        row = row + 1

        'since we're letting someone else tell us what value to stop at, maybe we should add some infinite loop protection
        If row = 1000 Then GoTo EscapeHatch
    Loop

    'push down one column to start reading just under the headers
    row = row + 1

    'scan down through the table until we hit some whitespace
    Do While Cells(row, 1).Text <> ""

        'in each iteration, grab every 3rd column from col 2 and assign it a color
        '  according to the code in the first cell.
        For col = 2 To 14 Step 3
            Cells(row, col).Interior.Color = Cells(Cells(row, 1) + 15, 2).Interior.Color

        Next col

        'increment the row
        row = row + 1
    Loop

EscapeHatch:
End Sub