VBA and Macros Condition

Myst3ryssss

New Member
Joined
Jul 28, 2011
Messages
3
Hi,(I will try in english).


I have a Excel document with two sheets inside.
I have a input form (Sheet "Ajouter"):
34392597.jpg


This form input my entrys in this little database (Sheet "Mouvement")
67338735.jpg


My input Macro is:
Code:
Sub nouvelle_valeur()
'
' nouvelle_valeur Macro
'
'
    Sheets("Mouvement").Select
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Sheets("Ajouter").Select
    Range("A2:D2").Select
    Selection.Copy
    Sheets("Mouvement").Select
    Range("A2:D2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Range("E2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Range("E3").Select
    Sheets("Ajouter").Select
    Range("C10").Select
    Selection.ClearContents
    Range("C7").Select
    Selection.ClearContents
    ActiveWorkbook.RefreshAll
End Sub

Okay, the problem is the next one:
If I put 0$ in the "Valeur Marchande" field and then I press "Insérer", the entry will get input like this:

89389437.jpg


As you can see, it make unbalance the "profit/perte" colum. So, I want to know if it's possible to add something in the input macro that make it check if the "valeur marchande" field is = 0$:
- IF no, the entry is add normally
- IF yes, the macro go get the last most recent value of "Valeur Marchande" in the "Mouvement" sheet, for the good account (compte).

Exemple:
I select the account "CELI", then I put 0$ in the "Valeur marchande" field and then, I press "insérer".
The macro detect that "valeur Marchande" = 0. It go take the most recent value for the "CELI" account in my "mouvement" Sheet and replace the 0$ by this price.

I have already make a little formula (not in VBA) to try to see how I can get this "most recent value", and i got this:

Code:
=SI(Ajouter!C10=0;RECHERCHEV(B2;Mouvement!RECHERCHE_PLAGE_VAL0;3;FAUX))

RECHERCHE_PLAGE_VAL0
Code:
=DECALER(Mouvement!$A$1;1;1;NBVAL(Mouvement!$A$1:$A$20)-1;4)

This is work 50/50. If I put 0$ in C10 it's ok and the formula show the good amount. If I put 1$ then it return me "FAUX".


So I have 3 question:
  1. How can I change this "FAUX" return...for "do nothing"?
  2. How can I "transform" this RECHERCHEV formula to make it fit in my macro?
  3. How can I "create" a fonction that will replace the 0$ value by the RECHERCHEV return value?
Thanks alot and sorry for this poor english,
Maxime

P.s. Don't worry, you can post your formula in english, I'll make the translation myself.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top