简体   繁体   中英

How to avoid a circular reference with VBA in this case?

I'm entering many rows of data. Sometimes I'll enter net in column A, and sometimes I'll gross in column C. Net + VAT = Gross. In all cases I want the formulas to calculate the missing numbers for me automatically.

I have three columns (ABC) column A represents Net, B represents nominal VAT, and C Gross. I have a formula in B that if A VAT, or if B VAT else blank.

Currently, if I put anything in A, the VAT is calculated and C is blank:

  A    B    C
  100  20  

And equally, if I put anything in C, the VAT is calcualted but A is blank:

A    B    C
    20    120

What I want is below, whether it is A that is given, or C.

A    B    C
100  20   120

I am trying to Calculate Net given gross figure, and gross given net figure on a single row.

Note, column B is protected, I have a formula that calculates the VAT automatically whether a figure is entered into C first, or A first:

=IF(E2>0,E2*VATRATE,IF(G2>0,G2-(G2/(1+VATRATE)),""))

Ideally with VBA, if A given then cal C, and if C given calc A.

Actually you don't need VBA at all for this. Use these formulas:

  • Net in A2 =IF(NOT(ISFORMULA(C2)),C2-B2,0)
  • VAT in B2 =IF(NOT(ISFORMULA(A2)),A2*VATRATE,IF(NOT(ISFORMULA(C2)),C2-(C2/(1+VATRATE)),0))
  • Gross in C2 =A2+B2

The trick is to determine whether a number was manually entered. Once you know that, you can use if statements to avoid a circular reference.

Copy down for as many lines as you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM