Code creates its own If/For statements

  • Hi folks

    Does anybody know how to make your code dynamically create its own 'For' and 'If' statements?

    I need my code to create a 'For' statement followed by an 'If' statement whenever the 'If' statement is satisfied.

    For a = 1 To 10
    If Variable1 = 3 Then
    (Create new....For b = 1 To 10
    If Variable2= 3 then..............)

    The reason I need this is because sometimes the conditions in my code will mean that I need many 'For' and 'If' statements within each other and sometimes I will only need one.

    Am I walking into a brick wall here?


  • Re: Code creates its own If/For statements

    If you believe you really do need to dynamically generate your code you could try something like this:

    Create a new module in the workbook that contains the code you are running and call it 'DynamicModule'. Enter the following code in this module:

    Sub DynamicSub()
    End Sub

    Then in your original code use something like this:

  • Re: Code creates its own If/For statements

    Thanks to everyone for their advice

    I've got it working a treat.

    Many, many thanks

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!