Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

excel - IF AND formula with variable VBA

Could you please tell me what needs to be updated in the formula below so it's working?

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function
COMPCOLLETTER = Col_Letter(Application.Match("Comparison Percentage", Rows(1), 0))

'actual formula (desired result)
'=IF(AND(F2<=5%,F2>=-5%),"no check required","")

'I want to add a variable COMPCOLLETTER which is a column letter
'but something is wrong in here:
Cells(2, 10).Formula = "=IF(AND((" & COMPCOLLETTER & "2<=5%,(" & COMPCOLLETTER & "2>=-5%),"no check required","")"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

These tasks can be tricky when you have embedded quotes (which need to be doubled up). Properly building the string results in this:

"=IF(AND(" & COMPCOLLETTER & "2<=5%," & COMPCOLLETTER & "2>=-5%),""no check required"","""")"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...