Results 1 to 4 of 4

Thread: How to assign array to a MSflexgrid

  1. #1

    Thread Starter
    Addicted Member thiru_rajamani's Avatar
    Join Date
    Aug 2004
    Location
    Chennai,India
    Posts
    214

    How to assign array to a MSflexgrid

    I have a array as delared following, but I do not know how to assign them to a flexgrid.
    I have a MSflesxgrid with 5 rows and 5 columns.


    Private Sub UserForm_Initialize()
    Dim i, j
    Dim xx(5, 5)

    For i = 0 To 4
    For j = 0 To 4
    xx(i, j) = i & j
    Debug.Print xx(i, j)
    Next
    Next
    End Sub
    Thanks
    Raj

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to assign array to a MSflexgrid

    You can set the text of the cells using TextMatrix() in a loop, like this:
    VB Code:
    1. For i = 0 To 4
    2.   For j = 0 To 4
    3.     MsFlexgrid1.TextMatrix(i,j) = xx(i, j)
    4.   Next
    5. Next
    Depending on how your grid is set up (fixed rows/columns etc) you may want to change (i,j) to (i+1,j) or (i,j+1) etc.

    You could do it in the same loops you already have (just replace the "Debug.Print xx(i, j)" with the TextMatrix line).


    By the way, you should declare your variables with data types, as it eliminates chances for hidden errors/mistakes. In this case it should be:
    VB Code:
    1. Dim i as Integer, j as Integer
    2. Dim xx(5, 5) as String

  3. #3

    Thread Starter
    Addicted Member thiru_rajamani's Avatar
    Join Date
    Aug 2004
    Location
    Chennai,India
    Posts
    214

    Re: How to assign array to a MSflexgrid

    I understand. and I am using the same what you suggested. But I feel it consumes more time. Is there anyother way to input the data quickly ?
    Thanks
    Raj

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to assign array to a MSflexgrid

    The easy way of speeding up any work with a Flexgrid is to stop the re-painting of the control every time something changes.

    Just put this before the loop:
    MsFlexgrid1.Redraw = False

    And this after the loop:
    MsFlexgrid1.Redraw = True

    It should be 10-50% quicker, depending on the formatting and contents of the grid.


    A faster way of actually putting the data in is to use the .Clip property of the grid, but to do that you need to format the text first (a tab between each column, and a CR between each row), which could possibly take more time than just putting it straight into the grid.

    In your case you could do this:
    VB Code:
    1. Dim sClip as String
    2. sClip = ""
    3. For i = 0 To 4
    4.   For j = 0 To 3
    5.     sClip = sClip & xx(i, j) & vbTab
    6.   Next j
    7.   sClip = sClip & xx(i, 4) & vbCr
    8. Next i
    9. With MsFlexGrid1
    10.   .Row = 0    '("select all")
    11.   .Col = 0
    12.   .RowSel = 4
    13.   .ColSel = 4
    14.   .Clip = sClip  '("paste")
    15.   .RowSel = 0  'reset selection
    16.   .ColSel = 0
    17. End With
    Obviously this method lends itself more to situations where you can get the data in the right format to start with.

Posting Permissions

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



Click Here to Expand Forum to Full Width