PDA

View Full Version : Sleeper: Cascade String concatenation problem



AAPLNVDA
01-03-2020, 03:20 PM
Hi everyone, I am pretty new to vba but I have been coding for a long time in c/c++. What I want to do is :
When I write on a selected cell for example INTC I want the cell right next to it to display sometihng like FIBBONACCI('INTC') <= 50

So what I did is : use Worksheet with change and this is the code :


Private Sub Worksheet_Change(ByVal Target As Range)
Dim SymbolName As String
SymbolName = Target.Value
ActiveCell.Offset(-1, 1) = "FIBBONACCI(" & SymbolName & " ) <= 50"
End Sub

But the result keeps cascadiing :


FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACC I(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONA CCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBO NACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIB BONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(F IBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI (FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(FIBBONACCI(intc ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50 ) <= 50

What is wrong in my code? Thank you very much

AAPLNVDA
01-03-2020, 03:36 PM
This is the unwanted result what I want is only FIBBONACCI('INTC') <= 50 Thanks again

Paul_Hossler
01-03-2020, 04:13 PM
This code was event driven and each time you changed the WS it was called again
Temporarily disable events with Application.EnableEvents = False


Use Target.Offset

.Offset should be (0,1) meaning 'same row but one column to the right'




Private Sub Worksheet_Change(ByVal Target As Range)
Dim SymbolName As String

SymbolName = Target.Value

Application.EnableEvents = False
Target.Offset(0, 1) = "FIBBONACCI(" & SymbolName & " ) <= 50"

Application.EnableEvents = True
End Sub

Didn't test