将数据存储在数组中,隐藏某些行并将数据写回非隐藏行

时间:2014-01-14 23:27:33

标签: excel-vba vba excel

在名为“EIRP LL”的工作表中,范围L6:O13包含数据。有时,由于与此数据无关的原因,第7-13行被隐藏。范围L6:O13中的数据应保持未隐藏状态,因此L6:O13中的数据将复制到名为ConfigDataArray的数组中。范围L6:然后清除O13。所有这些代码都有效。

然后,困难开始了。存储在ConfigDataArray中的数据必须写入从第6行开始的非隐藏行,这些行总是被取消隐藏。我试图通过切片数组的行并使用For循环遍历这些行来完成此操作。但它不起作用。只有数组数据的第1行和第3行被写回工作表,第3行被写入隐藏行。以j = 6开头并以Next结尾的代码显然是错误的。任何建议都非常感谢。

Sub HideLLRows()
'Hide blank rows in EIRP LL
'Where blank row is defined as no data in Col B for the given row
Application.ScreenUpdating = False

Dim ConfigDataArray As Variant
Set EIRPLL = Sheets("EIRP LL")
LastLLRow = EIRPLL.UsedRange.Rows.Count

'Put the metadata into an 8Row x 4Col array for safe keeping
ConfigDataArray = Range("L6:O13").Value

'Clear the metadata cells
Range("L6:O13").Clear

'Hide the blank rows
For i = 6 To LastLLRow
    If EIRPLL.Range("B" & i) = "" Then
        EIRPLL.Rows(i).Hidden = Not EIRPLL.Rows(i).Hidden
    End If
Next

'Slice the 8 array rows and put into the first 8 non-hidden rows
'beginning on L6:O6 (which is always non-hidden)

j = 6
For k = 1 To 8
    If Rows(j).Hidden = False Then
        If k < 9 Then
            EIRPLL.Range("L" & k + 5) = Application.Index(ConfigDataArray, k, 1)
            EIRPLL.Range("M" & k + 5) = Application.Index(ConfigDataArray, k, 2)
            EIRPLL.Range("N" & k + 5) = Application.Index(ConfigDataArray, k, 3)
            EIRPLL.Range("O" & k + 5) = Application.Index(ConfigDataArray, k, 4)
        End If
    End If
    k = k + 1
    j = j + 1
Next

Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:1)

Sub HideLLRows()

Dim ConfigDataArray As Variant, i, k, j
Dim EIRPLL As Worksheet, LastLLRow

    Set EIRPLL = Sheets("EIRP LL")

    LastLLRow = EIRPLL.UsedRange.Rows.Count

    Application.ScreenUpdating = False

    'Clear the metadata cells
    With EIRPLL.Range("L6:O13")
        ConfigDataArray = .Value
        .Clear
    End With

    'Hide the blank rows
    For i = 6 To LastLLRow
        If EIRPLL.Range("B" & i) = "" Then
            EIRPLL.Rows(i).Hidden = True
        End If
    Next

    k = 1
    j = 6
    Do While k <= 8
        With EIRPLL.Rows(j)
            If Not .Hidden Then
                .Cells(12).Value = ConfigDataArray(k, 1)
                .Cells(13).Value = ConfigDataArray(k, 2)
                .Cells(14).Value = ConfigDataArray(k, 3)
                .Cells(15).Value = ConfigDataArray(k, 4)
                k = k + 1
            End If
        End With
        j = j + 1
    Loop

    Application.ScreenUpdating = True
End Sub