The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

Hi,

I have a worksheet as below:

columnA columnB columnC columnD columnE

48.00 44.00 44.00 48.00

in the VBE, i will create a macro that will insert a formula into columnE.

this is my formula:

cells(1,5).formula="=IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE(A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))"

(the value in cells(1,5) should be 46.00)

then i write a coding to change the value in cells(1,5) :

if cells(1,5) mod 4 <1 then

cells(1,5).formula="=IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE

(A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))"

elseif cells(1,5) mod 4 <2 then

cells(1,5).formula="=IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE

(A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))-1"

elseif cells(1,5) mod 4<3 then

cells(1,5).formula="=IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE

(A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))+2"

else

cells(1,5).formula="=IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE

(A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))+1"

end if

The coding above meaning it will round the number.

For example, if the value is 5, it will be rounded to 4

if the value is 7, it will be rounded to 8

if the value is 6, it will be rounded to 8

if the value is 3, it will be rounded to 4

so, after i run the macro, the final value for cells(1,5) should be 48.00. Agree?

Now the value in cells(1.5) is 48.00, it will add 2 to the 46.00.

The problem is when i change the value in cells(1,1) to 100.00, the value in cells(1,5) is 61.00. By right the value should be 60.00. What can i do to the coding so that i will get the value of 60.00 in cells(1,5) when i change the value in cells(1,1) to 100. (Meaning i don't wan to run the macro again, i just have to run the macro once, and in the future, when i change the value in cells(1,1), it will gives me the correct value for the cells(1,5) ).

Anyone know how to do it?

cheers,

brandon

I have a worksheet as below:

columnA columnB columnC columnD columnE

48.00 44.00 44.00 48.00

in the VBE, i will create a macro that will insert a formula into columnE.

this is my formula:

cells(1,5).formula="=IF(AN

(the value in cells(1,5) should be 46.00)

then i write a coding to change the value in cells(1,5) :

if cells(1,5) mod 4 <1 then

cells(1,5).formula="=IF(AN

(A1:D1)),ROUNDUP(AVERAGE(A

elseif cells(1,5) mod 4 <2 then

cells(1,5).formula="=IF(AN

(A1:D1)),ROUNDUP(AVERAGE(A

elseif cells(1,5) mod 4<3 then

cells(1,5).formula="=IF(AN

(A1:D1)),ROUNDUP(AVERAGE(A

else

cells(1,5).formula="=IF(AN

(A1:D1)),ROUNDUP(AVERAGE(A

end if

The coding above meaning it will round the number.

For example, if the value is 5, it will be rounded to 4

if the value is 7, it will be rounded to 8

if the value is 6, it will be rounded to 8

if the value is 3, it will be rounded to 4

so, after i run the macro, the final value for cells(1,5) should be 48.00. Agree?

Now the value in cells(1.5) is 48.00, it will add 2 to the 46.00.

The problem is when i change the value in cells(1,1) to 100.00, the value in cells(1,5) is 61.00. By right the value should be 60.00. What can i do to the coding so that i will get the value of 60.00 in cells(1,5) when i change the value in cells(1,1) to 100. (Meaning i don't wan to run the macro again, i just have to run the macro once, and in the future, when i change the value in cells(1,1), it will gives me the correct value for the cells(1,5) ).

Anyone know how to do it?

cheers,

brandon

like:

"if(mod(" & cells(1,5).value & ",4)<1," & _

"IF(AND(MOD(AVERAGE(A1:D1)

(A1:D1)),ROUNDUP(AVERAGE(A

",if(mod("& cells(1,5).value & ",4)<2," &

"IF(AND(MOD(AVERAGE(A1:D1)

(A1:D1)),ROUNDUP(AVERAGE(A

and so on.

you can include the old cells value in your formula "the hard way", and thus, it should work.

ok, you formula gets a little bit longer, but whatever..

What is the meaning og the "target.column" ?

Where the parameter that pass to this function come from?

cheers,

brandon

in vbe press F1 go to answer wizard and make a search for type application object events.

btw did it solve the problem

All Courses

From novice to tech pro — start learning today.

the problem is that macors dont monitor sheets for changes. so when you change the value of A1 to 100 sheet E1 will contain the previous foumula which calculates it to 61. running the macro again will change the foumula which will change it to 60.

to automate this process so that you dont have to run the macro again copy this code the workbook_change function of your work book. this code recalculates the formula whenever a value in any of the 4 columns of first row is changed

(in your workbook hit ALT+F11 and paste the following code and return to excel)

------------

Private Sub Workbook_SheetChange(ByVal

If Target.Column < 5 And Target.Row = 1 Then

Cells(1, 5).Formula = "=IF(AND(MOD(AVERAGE(A1:D1

"TRUNC(AVERAGE(A1:D1)),ROU

If Cells(1, 5) Mod 4 < 1 Then

Cells(1, 5).Formula = "=IF(AND(MOD(AVERAGE(A1:D1

"TRUNC(AVERAGE(A1:D1)),ROU

ElseIf Cells(1, 5) Mod 4 < 2 Then

Cells(1, 5).Formula = "=IF(AND(MOD(AVERAGE(A1:D1

"TRUNC(AVERAGE(A1:D1)),ROU

ElseIf Cells(1, 5) Mod 4 < 3 Then

Cells(1, 5).Activate

ActiveCell.Formula = "=IF(AND(MOD(AVERAGE(A1:D1

"TRUNC(AVERAGE(A1:D1)),ROU

Else

Cells(1, 5).Formula = "=IF(AND(MOD(AVERAGE(A1:D1

"TRUNC(AVERAGE(A1:D1)),ROU

End If

End If

End Sub

-------------