Here is how the data is presented (it's in pivot table style but not a true pivot table):
and this is how I need it to transform to:
For each visit, which always start in column H and needs to finish before 'Total Activity Costs' (hence why I used offset of 5), I need information duplicated. I only need the rows for each visit to be present if there is a 1 against that row for that particular visit. In the second image, you can see that not all rows are present for each visit as they don't have a 1 in their column.
Some of the data is hard coded and other columns come from another sheet, as you'll see in the code.
The code I have written so far copies across to the "BuiltCosts" sheet but each visit is being overwritten each time the loop is run. I can see it's due to p=2.... but having Googled a lot, I simply cannot work out how to achieve my goal.
Here is the code for this particular part of the module:
With ActiveSheet
Dim lrrg As Range ' Do I need this?
Set lrrg = Range("A1").CurrentRegion 'Do I need this?
Dim lastcolumn As Long, lastrow As Long
Dim p As Long
Dim VisitCount As Long, VisitRange As Range
lastrow = lrrg.Rows(lrrg.Rows.Count).Row + 1
' I need to use row 5
lastcolumn = _
ActiveSheet.Cells(5, ActiveSheet.Columns.Count).End(xlToLeft).Column
'In row 5, column H to last column
Set VisitRange = Range(Cells(5, 8), Cells(5, lastcolumn).Offset(0, -5)) 'I need the last column to be 5th from the far right
Dim Visit As Range
For Each Visit In VisitRange
If Visit.Value <> "" Then
For p = 2 To lastrow
Sheets("BuiltCosts").Cells(p, 1) = Worksheets("Front_Page").Range("D8")
Sheets("BuiltCosts").Cells(p, 2) = Worksheets("Front_Page").Range("D22") & "" & Worksheets("Front_Page").Range("G14") & " " & ActiveSheet.Range("C1") & " " & Visit
Sheets("BuiltCosts").Cells(p, 3) = "Participant"
Sheets("BuiltCosts").Cells(p, 6) = .Cells(p, 1) & " " & .Cells(p, 5)
Sheets("BuiltCosts").Cells(p, 8) = "Research Cost"
Sheets("BuiltCosts").Cells(p, 11) = .Cells(p, 3)
Sheets("BuiltCosts").Cells(p, 13) = .Cells(p, 6)
'*************The line below needs to be dynamic, so rather than .Cells(p, 8),
'I need each column in turn to be used when it loops through the columns.
'8 is the first column of the '1's that I want
'Sheets("BuiltCosts").Cells(p, 14) = .Cells(p, 8)
Next p
End If
Next Visit
End With


BuiltCosts_pointer) assign a start value (BuiltCosts_pointer = 2) and increment within theFor p = 2 To lastrowby 1. Use this variable on all theSheets("BuiltCosts").Cells(BuiltCosts_pointer, 1....13). If this is what you want.With ActiveSheetandBuiltCosts_pointer = BuiltCosts_pointer +1is beforeNext p. SIDENOTE: Referencing ActiveSheet is not a safe thing if not specifically need it. Better to useWorksheets(".......")structure to avoid incorrect reference.