Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

msflexgrid question

I have a grid  with the ff columns

unit cost
total cost
unit price
total price

I want to make the values -numbers negative in that row alone

How is that done?

Thanks
Avatar of dbrckovi
dbrckovi
Flag of Croatia image

Hi!

If I inderstood properly, you want to make all values in single row negative.
If so, then try this:
'------------------------------------------------
Private Sub Command1_Click()
    MakeNegative MSFlexGrid1, 3
End Sub

Sub MakeNegative(GridName As MSFlexGrid, RowNumber As Long)
   
    If RowNumber >= GridName.Rows Then
        MsgBox "Invalid Row"
    Else
        For X = 0 To GridName.Cols - 1
            GridName.TextMatrix(RowNumber, X) = 0 - Val(GridName.TextMatrix(RowNumber, X))
        Next X
    End If
End Sub
'---------------------------------------------------

If this is not what you need, then please explain a bit more.
 - What is 'ff' ?
 - Are unit cost, total cost, etc.,  Column headers or Row headers?
 - What does "values -numbers" mean?
Avatar of zachvaldez

ASKER

Are unit cost, total cost, etc.,  Column headers or Row headers? -- Columnheaders

What does "values -numbers" mean - input are numbers

I idea is to make the numbers appear negative in that row selected as if you are giving a discount for that row


Why am is the rownumber hardocoded here?

Private Sub Command1_Click()
    MakeNegative MSFlexGrid1, 3 ---->    Can it be identified based on row selected instead of hardcodind it...?
End Sub
Avatar of PePi
PePi

'declare a modulelevel variable
Private fRow as Integer, fCol as Integer


Private Sub flxGrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    With flxGrid
        If .MouseRow = 0 Then fRow = 0: Exit Sub

        fRow = .MouseRow: fCol = .MouseCol
    End With
End Sub

Private Sub flxGrid_Click()
    If fRow = 0 Then Exit Sub       'don't process if header was clicked

    With flexGrid
        If Not IsNumeric(.TextMatrix(fRrow, fCol)) Then
            MsgBox "Cannot Convert! Data Not Numeric.", vbOkOnly+vbExclamation, "Conversion Error"
            Exit Sub
        End if

        'continue with conversion
        .Redraw = False
        .TextMatrix(fRow, fCol) = "-" & .TextMatrix(fRow, fCol)
        .Redraw = True
    End With
End Sub

HTH!!!

Specifically, the location I want to convert are in col 7 & col 9

col 7 is unit cost col 9  unit price

However I do not make all of the values in this column negative only The selected line row.

col 1 to 6 are text ::
when you mean Columns 7 or 9, is that 0 based? i mean in your flexgrid, the first column is actually Col = 0. I assumed that by columns 7 and 9, they are 0 based. If not, just change 7 to 6 and 9 to 8

Private Sub flxGrid_Click()
    If fRow = 0 Then Exit Sub       'don't process if header was clicked

    With flexGrid
        If fCol = 7 Or fCol = 9 Then
            If Not IsNumeric(.TextMatrix(fRrow, fCol)) Then
                MsgBox "Cannot Convert! Data Not Numeric.", vbOkOnly+vbExclamation, "Conversion Error"
                Exit Sub
            End if

            'continue with conversion
            .Redraw = False
            .TextMatrix(fRow, fCol) = "-" & .TextMatrix(fRow, fCol)
            .Redraw = True
        End If
    End With
End Sub
When it hits this line it exits...

 If fCol = 7 Or fCol = 9 Then
add this line before the if statement

MsgBox "fCol = " & fCol


tell me what column you cliked and what was displayed
i guess Col 7 is actually Col 6 in 0 base, and Col 9 is Col 8 also in 0 base. to verify this, click on column 7 and if the display is "fCol = 6" then change the IF statement to:

If fCol = 6 Or fCol = 8 Then


This is what I improvise out of your code and let me know if you can improve it...

It seems that it is changing those numbers!


 Dim iCols As Integer
 
   
    For iCols = 0 To Grid1.Cols - 1
        With Grid1
           
            If iCols = 7 Then
                .TextMatrix(.row, 7) = .TextMatrix(.row, iCols) * -1
               
            End If
            If iCols = 9 Then
                .TextMatrix(.row, 9) = .TextMatrix(.row, iCols) * -1
               
            End If
        End With
    Next iCols
i thought you want it to change only when the row is clicked? which event did you put this code in?
command_click

Call ChangeNumtoNegative


end sub

Isa it possible to do this in one line...

         If iCols = 7 Then
                .TextMatrix(.row, 7) = .TextMatrix(.row, iCols) * -1
               
            End If
            If iCols = 9 Then
                .TextMatrix(.row, 9) = .TextMatrix(.row, iCols) * -1
               
            End If
        End With
yes,

if iCols = 7 or iCols = 9 then

end if
ASKER CERTIFIED SOLUTION
Avatar of PePi
PePi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can also change your for next loop statement to:


For iCols = 7 To 9 Step 2

Next iCols

coz you know for a fact that you only need to process columns 7 and 9. you don't have to go thru every column
Thanks! Thas was a great learning tip!