Consulting

Results 1 to 13 of 13

Thread: Solved: make ALL labels invisible (visible=false)

  1. #1
    VBAX Newbie
    Joined
    Dec 2007
    Posts
    5
    Location

    Solved: make ALL labels invisible (visible=false)

    OK here my problem: On my Excel sheet I have several buttons, lists and labels. What these buttons should do is make a label visible and every button makes a different label visible. What I could do is this:
    [vba]label1.visible = true
    label2.visible = false
    label3.visible = false
    etc. [/vba]
    and for the next button:
    [vba]label1.visible = false
    label2.visible = true
    label3.visible = false
    etc. [/vba]
    but because I have a lot of buttons and labels, that would be a lot of text and I really don't like to type anything more than needed. So what I am looking for is a peace of code that turns ALL the labels off in one click. This way I would get something like this:
    [vba]label(all).visible = false
    label1.visible = true

    label(all).visible = false
    label2.visible = true[/vba]
    This would be only 2 lines for each button instead of 16 lines.
    Hope any of you have an idea, thanx.


    [edit] edited the layout a bit, so other people can make sense of it to [/edit]
    Last edited by razer; 12-18-2007 at 03:08 PM.

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    try this:
    [vba]
    Sub label_hide()
    Dim i As Integer
    For i = 1 To 100
    On Error GoTo Nxt
    ActiveSheet.Shapes("Label" & i).Visible = False
    Next
    Nxt:
    End Sub
    [/vba]
    Last edited by Simon Lloyd; 12-18-2007 at 01:15 PM.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim shp As Shape

    For Each shp In ActiveSheet.Shapes
    If Left(shp.Name, 5) = "Label" Then
    shp.Visible = False
    End If
    Next shp
    ActiveSheet.Labels("Label 2").Visible = True
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Nice Bob, but are there advantages or disadvantages from using either yours or my code?, one i can think of is that mine needs to have the error handling (for some reason i couldnt COUNT the labels on the active sheet to determine For i = 1 to ActiveSheet.Labels.Count)
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No particular issue than I can see Simon, apart the error trap.

    But you can avoid that, you use a different collection

    [vba]

    Activesheet.Labels.Count
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Newbie
    Joined
    Dec 2007
    Posts
    5
    Location
    OK, this is what I ended up with that worked:

    [vba]Private Sub CommandButton4_Click()
    Dim shp As Shape

    For Each shp In ActiveSheet.Shapes
    If Left(shp.Name, 5) = "Label" Then
    shp.Visible = False
    End If
    Next shp
    Label1.Visible = True
    End Sub[/vba]
    So, thanx for your help guys, and I will be back here for some more help I think. I am just getting the hang of the simple VBA code and will be trying something harder in the future . So see you guys around.

  7. #7
    No, you are again writting TOO much code.
    Try :-
    [VBA]Private Sub AllInvis()
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
    If Left(shp.Name, 5) = "Label" Then shp.Visible = False
    Next shp
    End Sub

    PrivateSub CommandButton4_Click()
    Call AllInvis

    Label1.Visible = True
    End Sub


    PrivateSub CommandButton5_Click()
    Call AllInvis

    Label9.Visible = True
    End Sub


    PrivateSub CommandButton6_Click()
    Call AllInvis

    Label2.Visible = True
    End Sub


    ... etc.
    [/VBA]
    2+2=9 ... (My Arithmetic Is Mental)

  8. #8
    VBAX Newbie
    Joined
    Dec 2007
    Posts
    5
    Location
    OK thanx for that last code, it is a lot less typing .
    I ended up with the code below. I also edded some images that needed to switch on and off.

    [vba]Private Sub AllInvis()
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
    If Left(shp.Name, 5) = "Label" OR Left(shp.Name, 5) = "Image" Then shp.Visible = False
    Next shp

    End Sub

    Private Sub CommandButton100_Click()
    Call AllInvis
    Label100.Visible = True
    Image100.Visible = True
    End Sub

    Private Sub CommandButton101_Click()
    Call AllInvis
    Label101.Visible = True
    Image101.Visible = True
    End Sub

    ...etc
    [/vba]
    Do you guys wanna see what I'm trying to make? If so I'll post my Excel document. I must say I'm quit prode of it already.


    [edit] edited to an even shorter code [/edit]

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Go the whole hog

    [vba]

    Private Sub AllInvis(ByRef pzLabel, ByRef pzImage)
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
    If Left(shp.Name, 5) = "Label" Or Left(shp.Name, 5) = "Image" Then shp.Visible = False
    Next shp

    pzLabel.Visible = True
    pzImage.Visible = True

    End Sub

    Private Sub CommandButton100_Click()
    Call AllInvis(Label:=Label100, Image:=Image100)
    End Sub

    Private Sub CommandButton101_Click()
    Call AllInvis(Label:=Label101, Image:=Image101)
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  11. #11
    VBAX Newbie
    Joined
    Dec 2007
    Posts
    5
    Location
    OK ever shorter, nice job but you made a small mistake. It should be:

    [vba]Private Sub CommandButton100_Click()
    Call AllInvis(pzLabel:=Label100, pzImage:=Image100)
    End Sub[/vba]
    So with the pzLabel and pzImage

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    OK you got me, but at least I got the idea across. I started calling it Label and thought maybe that was not a good idea :-)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    VBAX Newbie
    Joined
    Dec 2007
    Posts
    5
    Location
    nope calling it Label is not a good idea. I tried that
    I did feel a bit stupid after I realized my mistake.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •