Consulting

Results 1 to 3 of 3

Thread: Changing code in runtime - why doesnt work

  1. #1
    VBAX Newbie
    Joined
    Feb 2007
    Posts
    1
    Location

    Changing code in runtime - why doesnt work

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I ran this code, with A1-A10 in B1:B10, and it ended up with

    [vba]

    If A10 then
    [/vba]

    so it worked fine for me
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •