PDA

View Full Version : Changing code in runtime - why doesnt work



balintov
06-18-2007, 11:36 AM
I wonder if any VBA-Superman can help me with this problem.
I use ?replaceline? in an excelfile to change the code of a module in runtime. First it works perfectly. The code grabs the desired string from an excel table and replaces the focus line in one of the codemodules functions. After that it runs that ?new? (changed) function and gets its value, passes it back in a table.
My huge problem is the following: It works only once (it would have to do that several times). Alltough the focus function changes every time due to replaceline (I can even see changing it) which is good, but its value is allways the same value as it was after the first change, regardeless of its meanwhile changed context, no matter how many times, after how many changes the cycle runs it.
How could I after every change newly reevaluate that function without interrupting the cycle?
Does anyone have an Idea?
(I made here a sketch to show how the logic works. In this example ThisIsIt-s value would be allways the same value as first. If first true then allways true, if first false then always false, no matter how the conditions and the function itself changes.)
Thanks

Function ThisIsIt() As Boolean

'here comes the row to change:

ThisIsIt = True
Else
ThisIsIt = False
End If

End Function
Sub EvaluateNew()
Dim i As Integer, dat As String

For i = 1 To 10
dat = Sheets(1).Cells(i, 3).Value
ChangeTheFunction Sheets(1).Cells(i, 2).Value
If ThisIsIt = True Then
Sheets(1).Cells(i, 1).Value = dat
End If
Next

End Sub
Sub ChangeTheFunction(cond As String)

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("ABEV").CodeModule
With VBCodeMod
.replaceline 9, _
"if " & cond & " Then"
End With

End Sub

Bob Phillips
06-18-2007, 02:07 PM
I ran this code, with A1-A10 in B1:B10, and it ended up with



If A10 then


so it worked fine for me

rbrhodes
06-18-2007, 07:48 PM
X post

http://www.ozgrid.com/forum/showthread.php?t=71250