PDA

View Full Version : Noob to vba - loop not looking at all



pvzshark
02-26-2020, 02:20 AM
I have 4 buttons - 2017 to 2020
then 12 buttons jan to desember.

When you click 2017 then i hide all cells that is in that range. the same for 2018 etc

then if you do not want to see jan you can click on jan then it should take jan away, but because the trigger is at hide and you click jan then the jan 2017 becomes unhidded as the 2017 has hidden it.

I have tried to fix it with this iff then statement but now it only reads the first iff statement. please help


Private Sub b2017_Click()
Range("EE2").Value = (Range("EE2").Value + 1) Mod 1
Me.OLEObjects(1).Object.Caption = IIf(Range("EE2").Value = 0, "20170", "2017a")
Set xAddress = Range("C:C,H:H,M:M,R:R,W:W,AB:AB,AG:AG,AL:AL,AQ:AQ,AV:AV,BA:BA,BF:BF,BK:BK,BQ:BQ")
If xAddress.EntireColumn.Hidden = True Then
xAddress.EntireColumn.Hidden = False
Else
xAddress.EntireColumn.Hidden = True
End If
End Sub



Private Sub ToggleButton1_Click()
Range("EF2").Value = (Range("EF2").Value + 1) Mod 1
Me.OLEObjects(16).Object.Caption = IIf(Range("EF2").Value = 0, "20180", "2018a")
Set xAddress = Range("D:D,I:I,N:N,S:S,X:X,AC:AC,AH:AH,AM:AM,AR:AR,AW:AW,BB:BB,BG:BG,BL:BL,BR:BR")
If xAddress.EntireColumn.Hidden = True Then
xAddress.EntireColumn.Hidden = False
Else
xAddress.EntireColumn.Hidden = True
End If
End Sub



Private Sub ToggleButton2_Click()
Range("EG2").Value = (Range("EG2").Value + 1) Mod 1
Me.OLEObjects(2).Object.Caption = IIf(Range("EG2").Value = 0, "20190", "2019a")
Set xAddress = Range("E:E,J:J,O:O,T:T,Y:Y,AD:AD,AI:AI,AN:AN,AS:AS,AX:AX,BC:BC,BH:BH,BM:BM")
If xAddress.EntireColumn.Hidden = True Then
xAddress.EntireColumn.Hidden = False
Else
xAddress.EntireColumn.Hidden = True
End If
End Sub
Private Sub ToggleButton3_Click()
Range("EH2").Value = (Range("EH2").Value + 1) Mod 1
Me.OLEObjects(3).Object.Caption = IIf(Range("EH2").Value = 0, "20200", "2020a")


Set xAddress = Range("F:F,K:K,P:P,U:U,Z:Z,AE:AE,AT:AT,AJ:AJ,AO:AO,AT:AT,AY:AY,BD:BD,BI:BI,BN:BN")
If xAddress.EntireColumn.Hidden = True Then
xAddress.EntireColumn.Hidden = False
Else
xAddress.EntireColumn.Hidden = True
End If
End Sub





Private Sub ToggleButton4_Click()
Range("EI2").Value = (Range("EI2").Value + 1) Mod 1
Me.OLEObjects(4).Object.Caption = IIf(Range("EI2").Value = 0, "JAN0", "JANa")
Set xAddress = Range("C:C,D:D,E:E,F:F,g:g")
Set a7Address = Range("C:C")
Set a8Address = Range("D:D")
Set a9Address = Range("E:E")
Set a10Address = Range("F:F")
If Range("ee2") = "0" And Range("ei2") = "1" Then
a8Address.EntireColumn.Hidden = True
ElseIf Range("ef2") = "0" And Range("ei2") = "1" Then
a7Address.EntireColumn.Hidden = True
ElseIf Range("eg2") = "0" And Range("ei2") = "1" Then
a9Address.EntireColumn.Hidden = True
ElseIf Range("eh2") = "0" And Range("ei2") = "1" Then
a10Address.EntireColumn.Hidden = True
ElseIf Range("ee2") = "1" And Range("ei2") = "1" Then
a7Address.EntireColumn.Hidden = True
ElseIf Range("ef2") = "1" And Range("ei2") = "1" Then
a8Address.EntireColumn.Hidden = True
ElseIf Range("eg2") = "1" And Range("ei2") = "1" Then
a9Address.EntireColumn.Hidden = True
ElseIf Range("eh2") = "1" And Range("ei2") = "1" Then
a10Address.EntireColumn.Hidden = True
ElseIf Range("ee2") = "1" And Range("ei2") = "0" Then
a7Address.EntireColumn.Hidden = True
ElseIf Range("ef2") = "1" And Range("ei2") = "0" Then
a8Address.EntireColumn.Hidden = True
ElseIf Range("eg2") = "1" And Range("ei2") = "0" Then
a9Address.EntireColumn.Hidden = True
ElseIf Range("eh2") = "1" And Range("ei2") = "0" Then
a10Address.EntireColumn.Hidden = True
ElseIf Range("ee2") = "0" And Range("ei2") = "0" Then
a7Address.EntireColumn.Hidden = False
ElseIf Range("ef2") = "0" And Range("ei2") = "0" Then
a8Address.EntireColumn.Hidden = False
ElseIf Range("eg2") = "0" And Range("ei2") = "0" Then
a9Address.EntireColumn.Hidden = False
Else
xAddress.EntireColumn.Hidden = True
End If
End Sub

werafa
03-01-2020, 04:13 PM
Hi pvzshark

you will help others to read your code if you use the # icon to wrap code tags around your vba code


Like this

also, your description of the problem is not clear. am I correct in understanding that you wish to hide/unhide groups of columns (eg all months in 2017), as well as individual columns (eg Jan 2017)
and when you hide 2017, and try to unhide Jan 2018, Jan 2017 also becomes visible?

if this is the case, it would seem that you need two tests to be true to unhide a column - year is visible = true, and month is visible = true
one easy way to apply this is to create helper rows, write the results of the visibility test into the relevant cell, and use these cells to control the hide/unhide process. you can hide the helper rows as needed.

werafa
03-01-2020, 04:17 PM
also,

regarding the elseif section, see if 'select case' will give you a better (more readable?) result.
also, what path does the code take when you step through the code with F8

cheers