dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Creating and using an array of objects in Excel 97

  1. #1
    Josh Guest

    Creating and using an array of objects in Excel 97


    Is there some way to emulate the effect of a control array in the Visual Basic
    included with Excel 97 and what version of VB is included w/ Excel 97?

    Here's an example of what I'm trying to do, but I have no idea what the correct
    way to do it is:

    Image = Array (image1,image2,image3)
    Image(1).Visible = True



  2. #2
    Sam Barrett Guest

    Re: Creating and using an array of objects in Excel 97


    Josh, the easiest way to do this is to use the Controls collection.
    First, create a UserForm with three image controls, Image1, Image2, and Image3.
    Now to make image #2 invisible:
    i = 2
    Controls("Image" & i).Visible = False

    No doubt, you will not like that solution, but there is a better way: you
    can create a collection and index it just like an array. First add a textbox
    control, TextBox1, to the UserForm and then enter this code:

    Option Explicit
    Dim cImages As New Collection

    Private Sub UserForm_Initialize()
    Dim c As Control
    For Each c In Controls
    If TypeOf c Is Image Then
    cImages.Add Item:=c, key:=Right(c.Name, 1)
    End If
    Next c
    End Sub

    Private Sub TextBox1_Change()
    Dim i As Integer
    i = CInt(Val(TextBox1.Text))
    If i > 0 And i <= cImages.Count Then
    cImages(i).Visible = False
    End If
    End Sub

    Now when you run the UserForm, you can enter 1, 2 or 3 and that Image control
    will disappear. You still have to include the index to the control in the
    name, but it looks a little more like standard VB code.
    Hope this helps! -- Sam

    Sam Barrett, MTL Systems, Inc., mailto:sam@mtl.com
    Brainbench MVP, http://www.brainbench.com

    And the things that you have heard...commit these to faithful
    men who will be able to teach others also. 2 Timothy 2:2


    "Josh" <Josh.Kanownik@littletikes.com> wrote:
    >
    >Is there some way to emulate the effect of a control array in the Visual

    Basic
    >included with Excel 97 and what version of VB is included w/ Excel 97?
    >
    >Here's an example of what I'm trying to do, but I have no idea what the

    correct
    >way to do it is:
    >
    > Image = Array (image1,image2,image3)
    > Image(1).Visible = True
    >
    >



  3. #3
    Josh Guest

    Re: Creating and using an array of objects in Excel 97


    Thanks for the help. I ended up using option one, but I have another problem.

    I want to compare two of the images to see if they are the same. The statement
    I currently have is:

    If image1.Picture = image2.Picture Then

    But this is not working properly. The two images I am comparing will have
    different names, but I want to know if the picture is the same.



    "Sam Barrett" <NoSpamSam@mtl.com> wrote
    :
    >
    >Josh, the easiest way to do this is to use the Controls collection.
    >First, create a UserForm with three image controls, Image1, Image2, and

    Image3.
    > Now to make image #2 invisible:
    > i = 2
    > Controls("Image" & i).Visible = False
    >
    >No doubt, you will not like that solution, but there is a better way: you
    >can create a collection and index it just like an array. First add a textbox
    >control, TextBox1, to the UserForm and then enter this code:
    >
    >Option Explicit
    >Dim cImages As New Collection
    >
    >Private Sub UserForm_Initialize()
    >Dim c As Control
    > For Each c In Controls
    > If TypeOf c Is Image Then
    > cImages.Add Item:=c, key:=Right(c.Name, 1)
    > End If
    > Next c
    >End Sub
    >
    >Private Sub TextBox1_Change()
    >Dim i As Integer
    > i = CInt(Val(TextBox1.Text))
    > If i > 0 And i <= cImages.Count Then
    > cImages(i).Visible = False
    > End If
    >End Sub
    >
    >Now when you run the UserForm, you can enter 1, 2 or 3 and that Image control
    >will disappear. You still have to include the index to the control in the
    >name, but it looks a little more like standard VB code.
    >Hope this helps! -- Sam
    >
    >Sam Barrett, MTL Systems, Inc., mailto:sam@mtl.com
    > Brainbench MVP, http://www.brainbench.com
    >
    >And the things that you have heard...commit these to faithful
    >men who will be able to teach others also. 2 Timothy 2:2
    >
    >
    >"Josh" <Josh.Kanownik@littletikes.com> wrote:
    >>
    >>Is there some way to emulate the effect of a control array in the Visual

    >Basic
    >>included with Excel 97 and what version of VB is included w/ Excel 97?
    >>
    >>Here's an example of what I'm trying to do, but I have no idea what the

    >correct
    >>way to do it is:
    >>
    >> Image = Array (image1,image2,image3)
    >> Image(1).Visible = True
    >>
    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center