Solved

VBA problem

Posted on 2003-10-29
6
222 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:brandonsinger
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:drnick
ID: 9647970
maybe you can concatenate all the formulas together.
like:
"if(mod(" & cells(1,5).value & ",4)<1," & _
"IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE
                  (A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))" &_
",if(mod("& cells(1,5).value & ",4)<2," &
"IF(AND(MOD(AVERAGE(A1:D1),1)<0.1,AVERAGE(A1:D1)>1),TRUNC(AVERAGE
                  (A1:D1)),ROUNDUP(AVERAGE(A1:D1),0)))-1"

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..
0
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 345 total points
ID: 9653699
Hey !   brandonsinger,

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 Sh As Object, ByVal Target As Range)
If Target.Column < 5 And Target.Row = 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))"
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).Activate
ActiveCell.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
End If
End Sub
-------------
0
 

Author Comment

by:brandonsinger
ID: 9654464
R_Rajesh,
What is the meaning og the "target.column" ?
Where the parameter that pass to this function come from?

cheers,
brandon
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9654535
target.column gives you the column number of the cell thats being changed. say you changed the value in B4, here the column is 4 so target.column will contain 4 and B is second column so target.column will contain 2. As soon as you change any value a workbook_change event will be triggered, we can trap these events using worksheet of workbook funcitons and the parameters from these functions will contain various information regarding the current state of the application.

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

btw did it solve the problem
0
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9654599
here the column is 4 so target.column will contain 4 and B is second column so target.column will contain 2   <----------

in my previous post the above line should read

here the row is 4 so target.row will contain 4 and B is second column so target.column will contain 2
0
 
LVL 5

Expert Comment

by:drnick
ID: 9655680
what is wrong with concatenating the formulas and inserting the old value of cells(1,5)?
this'll do also and is simple
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
excel if statement syntax 6 39
How to convert JSON file to csv? 7 54
Outlook 2010 Archive 3 42
Need a poor man's PowerPoint 5 41
Introduction Perhaps more familiar to developers who primarily use VBScript than to developers who tend to work only with Microsoft Office and Visual Basic for Applications (VBA), the Dictionary is a powerful and versatile class, and is useful …
Some time ago I was asked to create a VBA function that would calculate a check digit for an input number, using the following procedure: First, sum up all the individual digits in the number If that sum value has more than one digit, then sum up …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now