如何查看每131行的特定范围以执行格式化

时间:2019-04-02 16:22:56

标签: excel vba dynamic range

我的标题似乎很难理解,对于这种情况,我深表歉意,但是本质上,对于我的宏,如果这些特定范围满足特定条件,我将尝试为特定范围的每131行设置特定格式。这听起来可能很奇怪,但是基本上我正在处理一个excel文件,该文件将每年不断更新。 excel文件具有每年的表格。每个表中都有特定的信息。每个表格位于此excel文件中每个表格大约20行的位置。文件中的某些行应仅由公式组成,而某些行应仅由硬编码数字/空单元格等组成。我将显示当前可用的代码,并且一旦显示代码,项目应变得更有意义,至少我希望如此。

Sub HardcodedVSFormulas_Nissan()
Dim cell As Range
Dim Ws As Worksheet


''FY18/19
Dim Range18_1, Range18_2, Range18_3, Range18_4, Range18_5, Range18_6, Range18_7, Range18_8, Range18_9, Range18_10, Range18_11, Range18_12, Range18_13, Range18_14, Range18_15, Range18_16, Range18_17, Range18_18, Range18_19, Range18_20, Range18_21, Range18_22, Range_23, Range_24 As Range
Dim Range18NoF_1, Range18NoF_2, Range18NoF_3, Range18NoF_4, Range18NoF_5, Range18NoF_6, Range18NoF_7, Range18NoF_8, Range18NoF_9, Range18NoF_10, Range18NoF_11, Range18NoF_12, Range18NoF_13, Range18NoF_14, Range18NoF_15, Range18NoF_16, Range18NoF_17, Range18NoF_18, Range18NoF_19, Range18NoF_20, Range18NoF_21, Range18NoF_22, Range18NoF_23, Range18NoF_24 As Range

''FY19/20
Dim Range19_1, Range19_2, Range19_3, Range19_4, Range19_5, Range19_6, Range19_7, Range19_8, Range19_9, Range19_10, Range19_11, Range19_12, Range19_13, Range19_14, Range19_15, Range19_16, Range19_17, Range19_18, Range19_19, Range19_20, Range19_21, Range19_22, Range_23, Range_24 As Range
Dim Range19NoF_1, Range19NoF_2, Range19NoF_3, Range19NoF_4, Range19NoF_5, Range19NoF_6, Range19NoF_7, Range19NoF_8, Range19NoF_9, Range19NoF_10, Range19NoF_11, Range19NoF_12, Range19NoF_13, Range19NoF_14, Range19NoF_15, Range19NoF_16, Range19NoF_17, Range19NoF_18, Range19NoF_19, Range19NoF_20, Range19NoF_21, Range19NoF_22, Range19NoF_23, Range19NoF_24 As Range

''FY18/19 Ranges with formulas
Set Range18_1 = Range("C1285:Q1285") '
Set Range18_2 = Range("C1289:Q1291") '
Set Range18_3 = Range("C1293:Q1300") '
Set Range18_4 = Range("C1305:Q1314") '
Set Range18_5 = Range("C1316:Q1319") '
Set Range18_6 = Range("C1324:Q1332") '
Set Range18_7 = Range("C1334:Q1334") '
Set Range18_8 = Range("C1339:Q1351") '
Set Range18_9 = Range("C1353:Q1353") '
Set Range18_10 = Range("C1358:Q1359") '
Set Range18_11 = Range("C1364:Q1364") '
Set Range18_12 = Range("C1368:Q1371") '
Set Range18_13 = Range("C1373:Q1376") '
Set Range18_14 = Range("C1381:Q1388") '
Set Range18_15 = Range("C1393:Q1393") '
Set Range18_16 = Range("C1395:Q1395") '
Set Range18_17 = Range("C1397:Q1397") '

Set Range18_18 = Range("O1293:Q1314") '
Set Range18_19 = Range("O1334:Q1351") '
Set Range18_20 = Range("O1353:Q1370") '
Set Range18_21 = Range("B1284:B1370") '
Set Range18_22 = Range("S1284:S1370") '

Set Range18_23 = Range("C1360:Q1363") '
Set Range18_24 = Range("C1365:Q1367") '


Set Range18NoF_1 = Range18_1.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_2 = Range18_2.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_3 = Range18_3.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_4 = Range18_4.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_5 = Range18_5.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_6 = Range18_6.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_7 = Range18_7.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_8 = Range18_8.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_9 = Range18_9.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_10 = Range18_10.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_11 = Range18_11.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_12 = Range18_12.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_13 = Range18_13.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_14 = Range18_14.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_15 = Range18_15.Cells.SpecialCells(xlCellTypeFormulas)


Set Range18NoF_16 = Range18_16.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_17 = Range18_17.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_18 = Range18_18.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_19 = Range18_19.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_20 = Range18_20.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_21 = Range18_21.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_22 = Range18_22.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_23 = Range18_23.Cells.SpecialCells(xlCellTypeFormulas)
Set Range18NoF_24 = Range18_24.Cells.SpecialCells(xlCellTypeFormulas)


''FY19/20 Ranges with Formulas
Set Range19_1 = Range("C1416:Q1416")
Set Range19_2 = Range("C1420:Q1422")
Set Range19_3 = Range("C1424:Q1431")
Set Range19_4 = Range("C1436:Q1445")
Set Range19_5 = Range("C1447:Q1450")
Set Range19_6 = Range("C1455:Q1463")
Set Range19_7 = Range("C1465:Q1465")
Set Range19_8 = Range("C1470:Q1482")
Set Range19_9 = Range("C1484:Q1484")
Set Range19_10 = Range("C1489:Q1490")
Set Range19_11 = Range("C1495:Q1495")
Set Range19_12 = Range("C1499:Q1502")
Set Range19_13 = Range("C1504:Q1507")
Set Range19_14 = Range("C1512:Q1519")
Set Range19_15 = Range("C1524:Q1524")
Set Range19_16 = Range("C1526:Q1526")
Set Range19_17 = Range("C1528:Q1528")

Set Range19_18 = Range("O1424:Q1445")
Set Range19_19 = Range("O1465:Q1482")
Set Range19_20 = Range("O1484:Q1501")
Set Range19_21 = Range("B1415:B1501")

Set Range19_23 = Range("C1491:Q1494")
Set Range19_24 = Range("C1496:Q1498")


Set Range19NoF_1 = Range19_1.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_2 = Range19_2.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_3 = Range19_3.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_4 = Range19_4.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_5 = Range19_5.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_6 = Range19_6.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_7 = Range19_7.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_8 = Range19_8.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_9 = Range19_9.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_10 = Range19_10.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_11 = Range19_11.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_12 = Range19_12.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_13 = Range19_13.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_14 = Range19_14.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_15 = Range19_15.Cells.SpecialCells(xlCellTypeFormulas)


Set Range19NoF_16 = Range19_16.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_17 = Range19_17.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_18 = Range19_18.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_19 = Range19_19.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_20 = Range19_20.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_21 = Range19_21.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_22 = Range19_22.Cells.SpecialCells(xlCellTypeFormulas)

Set Range19NoF_23 = Range19_23.Cells.SpecialCells(xlCellTypeFormulas)
Set Range19NoF_24 = Range19_24.Cells.SpecialCells(xlCellTypeFormulas)

For Each cell In Range18_1
        If Intersect(cell, Range18NoF_1) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell

    For Each cell In Range18_2
        If Intersect(cell, Range18NoF_2) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell

    For Each cell In Range18_3
        If Intersect(cell, Range18NoF_3) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_4
        If Intersect(cell, Range18NoF_4) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_5
        If Intersect(cell, Range18NoF_5) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_6
        If Intersect(cell, Range18NoF_6) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell

    For Each cell In Range18_7
        If Intersect(cell, Range18NoF_7) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_8
        If Intersect(cell, Range18NoF_8) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_9
        If Intersect(cell, Range18NoF_9) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_10
        If Intersect(cell, Range18NoF_10) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_11
        If Intersect(cell, Range18NoF_11) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_12
        If Intersect(cell, Range18NoF_12) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_13
        If Intersect(cell, Range18NoF_13) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_14
        If Intersect(cell, Range18NoF_14) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_15
        If Intersect(cell, Range18NoF_15) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_16
        If Intersect(cell, Range18NoF_16) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_17
        If Intersect(cell, Range18NoF_17) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_18
        If Intersect(cell, Range18NoF_18) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_19
        If Intersect(cell, Range18NoF_19) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_20
        If Intersect(cell, Range18NoF_20) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_21
        If Intersect(cell, Range18NoF_21) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell


    For Each cell In Range18_22
        If Intersect(cell, Range18NoF_22) Is Nothing And IsEmpty(cell) = False Then
            cell.Interior.Color = 50000
        End If
    Next cell

使用此代码,可以看到Range18_1和Range19_1相距131行,而Range 19_2和Range19_2的情况相同。每个表的所有这24个范围应分开131行。此部分代码仅查看应仅由公式组成的行(除非单元格为空且不包含任何内容),因此,如果某些单元格不是公式或空单元格,则这些指定范围内的这些单元格将变为绿色。这指示用户修复这些绿色单元格以使其成为公式,而不是硬编码数字。显然,我现在所拥有的效率低下,因为总是在该excel文件中创建新表,因此我很好奇是否有一种方法可以自动在这些特定范围内自动查找每131行并执行我编写的条件。

也许还不清楚,我要帮忙的一个例子是先看C1285:Q1285,然后看C1416:1416,再看C1547:Q1547,依此类推。每131行查看一次。在VBA中甚至有可能做到这一点,因为我一无所知,但会有所帮助。

0 个答案:

没有答案