I am using the following code to output data in an email to a dynamic table where the number of table rows varies depending on the data. As the code stands it works correctly however when i save it in Marketing Cloud (Email) it moves the main AMPScript code above the table tag causing the AMPScript to break and output the header row for each data row which i dont want.

Is there a way to correct this?

  %%[var @rows, @row, @rowCount, @numRowsToReturn, @lookupValue, @i 
  set @lookupValue = AttributeValue("_SubscriberKey") /* value from attribute or DE column in send context */ 
  set @numRowsToReturn = 0 /* 0 means all, max 2000 */ 
  set @rows = LookupOrderedRows("ExampleDE",@numRowsToReturn,"Name, ID, Amount", "Subscriber Key", @lookupValue) 
  set @rowCount = rowcount(@rows) if @rowCount > 0 then for @i = 1 to @rowCount do var @DEColumn1, @DEColumn2, @DEColumn3,
  set @row = row(@rows,@i) /* get row based on counter */ 
  set @DEColumn1 = field(@row,"Name") 
  set @DEColumn2 = field(@row,"ID") 
  set @DEColumn3 = field(@row,"Amount") ]%%
    <td> %%=v(@DEColumn1)=%%</td>
    <td> %%=v(@DEColumn2)=%%</td>
    <td> %%=v(@DEColumn3)=%%</td>
  %%[ next @i ]%%
%%[ else ]%% 
                %%[ endif ]%%

  • Have you tried placing html comments around the AMPScript? Feb 12, 2020 at 2:22
  • 1
    I'd recommend using HTML-only content blocks. Feb 12, 2020 at 3:39

1 Answer 1


It sounds like you may be housing this code within a free-form content block. If so, the wysiwyg editor has been known to alter or move scripting. Try using your code within an HTML-only content block instead.

  • 1
    Thanks moving it to the HTML only block has fixed it
    – cocopopps
    Feb 12, 2020 at 20:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .