2014-01-14 45 views
0

在名爲「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 
+0

你的描述的這兩個部分似乎是在衝突:「有時候,行6-13得到隱藏無關的這個數據的原因」和「必須寫入非隱藏的行開頭第6行,這恰好總是被隱藏「 –

+0

更正,謝謝。 – jmaz

回答

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 
+0

是的!你做什麼雖然解決了這個問題。對不起,我遲遲沒有回覆。這是我第一次涉足陣列,這有助於很多。非常感謝。 – jmaz