Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA formula using a changing cell 's value and a constant

Status
Not open for further replies.

JerryKlmns

IS-IT--Management
Feb 7, 2005
2,062
GR
Hi everyone.

In Excel VBA this
Code:
.Range("H" & iRow).Formula = "=G" & iRow & "- S" & iRow
works fine and at line 2 results in H2:=G2-S2
and also this
Code:
.Range("H" & iRow).Formula = "=G" & iRow & "- 0.77"
returns the same result (where S2=0.77)

When I change to this
Code:
.Range("H" & iRow).Formula = "=G" & iRow & "-" .Range("S"& iRow).Value
I expect H2:=G2-0.77 but I get an Application Error..

Why can't I use a cell's value inside a formula? Am I setting it the wrong way?

 
You miss "&":
Code:
.Range("H" & iRow).Formula = "=G" & iRow & "-" [!]&[/!] .Range("S"& iRow).Value
If you still have application error reported, there may be a problem with regional settings. In this case use rather FormulaLocal:
Code:
.Range("H" & iRow).FormulaLocal = "=G" & iRow & "-" & .Range("S" & iRow).Value

combo
 
Also if cell .Range("S" & iRow) is empty then you will only have half a formula and will throw the Application error. One of many possible ways around it could be

Code:
If IsEmpty(.Range("S" & irow).Value) Then
.Range("H" & irow).Formula = "=G" & irow & "-" & "0"
Else
.Range("H" & irow).Formula = "=G" & irow & "-" & .Range("S" & irow).Value
End If
 

Thanx combo. I saw that, after the completion of the project...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top