Results 1 to 8 of 8

Thread: code prob****RESOLVED****

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2004
    Posts
    100

    code prob****RESOLVED****

    I have flexgrid here.
    I want the system to substract maximum value and minimum value for every row in flexgrid.
    And the result will be displayed in column 'Julat'(pls see the attachment)

    My problem here is, the value for the column is 0(for every row).
    I cant figure out why is like that.
    Pls help.
    Thank U in advance.

    Snippet code as below :

    VB Code:
    1. Dim row As Long
    2.     Dim column As Long
    3.     Dim Max As Long
    4.     Dim Min As Long
    5.    
    6.     With MSFlexGrid7
    7.    
    8.      For row = 1 To .Rows - 1
    9.      
    10.          Max = Val(.TextMatrix(row, 2))
    11.          Min = Val(.TextMatrix(row, 2))
    12.          
    13.          'loop only for column 2 to column 5
    14.          For column = 2 To .Cols = 5
    15.          
    16.              If Min > Val(.TextMatrix(row, column)) Then
    17.                  Min = Val(.TextMatrix(row, column))
    18.              End If
    19.  
    20.              If Max < Val(.TextMatrix(row, column)) Then
    21.                  Max = Val(.TextMatrix(row, column))
    22.              End If
    23.              
    24.         Next
    25.        
    26.         .TextMatrix(row, 7) = Val(Max - Min)
    27.        
    28.      Next
    29.          
    30.     End With
    Attached Images Attached Images  
    Last edited by azrina; Mar 3rd, 2004 at 02:41 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    the problem is this line:
    VB Code:
    1. For column = 2 To .Cols = 5

    it should be:
    VB Code:
    1. For column = 2 To 5


    as you have it ".Cols = 5" is returning a boolean (True/False) value depending on whether the grid has 5 columns or not.

    This boolean value is converted into a number (-1 or 0) which is always less than 2, so the loop never starts, and the Max/Min are always both equal to their initial values.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I was just about to reply, whenI saw si's reply. *slapgeek*

    I was going to point out the .cols=5 thing.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    ha ha.. beat you to it! *slapfrog*


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2004
    Posts
    100
    I want to substract maximum value and minimum value which the values are from column 'Sampel1' to column 'Sampel5'(pls see attachment).
    My prob is, the result for the subtraction for column 'Julat' (pls see the attachment)
    is diiferent if I calculate manually.

    For instance( row 5) :
    Max value = 354
    Min value = 10.6
    Subtraction = 354 - 10.6 = 343.4
    The answer should be 343.4, not 343

    Row 6 :
    Max value = 21.3
    Min value = 2.5
    Subtraction = 21.3 - 2.5 = 18.8
    The answer should be 18.8, not 19

    I cant figure out what is wrong with the code.
    Please help.
    Thank U in advance.

    Snippet code :

    VB Code:
    1. 'add column
    2.       MSFlexGrid7.Cols = MSFlexGrid7.Cols + 1
    3.       MSFlexGrid7.TextMatrix(0, 6) = "Purata"
    4.       MSFlexGrid7.Cols = MSFlexGrid7.Cols + 1
    5.       MSFlexGrid7.TextMatrix(0, 7) = "Julat"
    6.  
    7.       'mean for column Purata
    8.       Dim X
    9.       For X = 1 To MSFlexGrid7.Rows - 1
    10.         MSFlexGrid7.TextMatrix(X, 6) = (Val(MSFlexGrid7.TextMatrix(X, 1)) + Val(MSFlexGrid7.TextMatrix(X, 2)) + Val(MSFlexGrid7.TextMatrix(X, 3)) + Val(MSFlexGrid7.TextMatrix(X, 4)) + Val(MSFlexGrid7.TextMatrix(X, 5))) / 5
    11.       Next
    12.  
    13.     'range for column Julat
    14.     Dim row As Long
    15.     Dim column As Long
    16.     Dim Max As Long
    17.     Dim Min As Long
    18.  
    19.     With MSFlexGrid7
    20.  
    21.      For row = 1 To .Rows - 1
    22.  
    23.          Max = Val(.TextMatrix(row, 1))
    24.          Min = Val(.TextMatrix(row, 1))
    25.  
    26.          'loop only for column 1 to column 5
    27.          For column = 1 To 5
    28.          
    29.              If Min > Val(.TextMatrix(row, column)) Then
    30.                  Min = Val(.TextMatrix(row, column))
    31.              End If
    32.  
    33.              If Max < Val(.TextMatrix(row, column)) Then
    34.                  Max = Val(.TextMatrix(row, column))
    35.              End If
    36.              
    37.              Dim h
    38.              h = Max - Min
    39.              .TextMatrix(row, 7) = h
    40.  
    41.         Next
    42.  
    43.      Next
    44.     End With
    Attached Images Attached Images  

  6. #6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2004
    Posts
    100
    It works..
    Thank u all for helping!!

  8. #8

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