2014-11-24 108 views
1

我正在使用excel工作簿,並且在其中一個工作表上是隱藏或顯示的行,具體取決於在另一個工作表中選擇的選項。結構看起來是這樣的顯示某些行但保留其他行隱藏

A 
1 
2 
3 
4 

B 
1 
2 
3 
4 

C 
1 
2 
3 
4 

當不得不隱藏所有A和B,A和C,A,B或C的用戶隱藏A和B或C(該選項的選項他們必須在B或C之間選擇)。他們也可以選擇隱藏每個字母下的單個行。行1,2和3.如果隱藏選項2被選中,每個字母下的所有「2」行都被隱藏。如果他們取消選中此選項,則所有2行再次出現。問題是顯示已經隱藏的字母的「2」行。

我遇到了心理障礙,但這是我所做的。 Psuedocode的可讀性,因爲現在我的代碼是凌亂的,我恨vba的樣子。無論如何,這是一個比語法問題更多的邏輯問題。

Property hiddenA As Bool get let 
Property hiddenB As Bool get let 
Property hiddenC As Bool get let 


OptionButton1.Click() 
    hiddenA = true 
    Hide A row and all rows associated with it 

OptionButton2.Click() 
    HiddenA = false 
    Show A row and all rows associated with it 
OptionButton3.Click() 
    HiddenB = false 
    HiddenC = true 
    Show B row and all rows associated with it 
    Hide C row and all rows associated with it 

OptionButton4.Click() 
    HiddenB = true 
    HiddenC = false 
    Hide B row and all rows associated with it 
    Show A row and all rows associated with it 



CheckBox1.Click() 
     if CheckBox1.value = false Then 
      Hide all "1" rows 
     Else 
      Show all "1" rows, but keep the "1"s under already hidden letters, hidden. 
      This is the problem. 

等等。還有用於顯示/隱藏所有2,3和4行的複選框。

+0

這裏是我的顯示/隱藏尋找一切 'sheetSource.Range( 「11:11,27:27,43:43,59:59,75:75」)。EntireRow.Hidden =真' 其中每個範圍是每個字母的「1」行。等等。 – 2014-11-24 14:12:27

+0

它總是字母和數字嗎?你總是知道哪個「標題」行?它們是靜態的嗎? – Chrismas007 2014-11-24 14:49:49

+0

是的,它們是靜態的。不,他們實際上不是字母和數字,我只是展示了結構。實際的數據對我正在進行的項目有意義。現在我已經採取瞭解決這個問題的非常懶惰和低效的方法。 'code' – 2014-11-24 15:39:52

回答

0

粗略輪廓 - 未測試代碼...類似這樣的東西應該取消隱藏所有內容,檢查每個複選框的狀態,將複選框添加到範圍,並在最後隱藏整個範圍。

'CheckBox1 is Row 1 in group 
'CheckBox2 is Row 2 in group 
'CheckBox3 is Row 3 in group 
'CheckBox4 is Row 4 in group 
'CheckBox5 is Row 5 in group 
'CheckBox6 is Group A 
'CheckBox7 is Group B 
'CheckBox8 is Group C 
'CheckBox9 is Group D 
'CheckBox10 is Group E 

Sub CheckBoxClick() 'Assign this to all checkboxes 
Application.ScreenUpdating = False 'Turn off screen updating 
ActiveSheet.Cells.EntireRow.Hidden = False 'Unhide all 

Dim RngCnt As Range 
Dim LastRow As Long 
Dim CurRow As Long 
Dim ChkBx As OLEObject 

LastRow = Range("A" & Rows.Count).End(xlUp).Row 
    For Each ChkBx In ActiveSheet.OLEObjects 
     If TypeName(ChkBx.Object) = "CheckBox" Then 
     Select Case ChkBx.Name 
      Case "CheckBox1" 
       If ChkBx.Value = True Then 
        RngCnt = Union(RngCnt, Range(Rows this chk box effects)) 
       End If 
      Case "CheckBox2" 
       If ChkBx.Value = True Then 
        RngCnt = Union(RngCnt, Range(Rows this chk box effects)) 
       End If 
      Case ETC, ETC, ETC to "CheckBox10" 
      ... 
     End If 
    Next ChkBx 
    RngCnt.EntireRow.Hidden = True 
Application.ScreenUpdating = True 
End Sub 
相關問題