Sub Button1_Click()
Dim i, j, k As Integer
Dim x(1, 0) As Variant
Dim y As Range
Dim rg, itemval, bleh, target As String
x(1, 0) = "<>"
Application.ScreenUpdating = False
Sheets("Output").Cells.Clear
ActiveWorkbook.Save
k = Application.WorksheetFunction.CountA(Sheets("Data").Range("A:A"))
For Each Item In Sheets("Data").Range("A1:A" & k)
Sheets("Output").Activate
Sheets("Output").Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
With Range("A" & ActiveCell.Row + 2)
.Value2 = Item.Value2
.Select
End With
bleh = ActiveCell.Offset(1, 0).Address
'column C, ABC variable
j = Application.WorksheetFunction.CountA(Sheets("Data").Range("C" & Item.Row &
":" & "ABC" & Item.Row))
target = bleh & ":" & Range(bleh).Offset(0, j - 1).Address
Sheets("data").Activate
Set y = Range(Cells(Item.Row, 3), Cells(Item.Row, j + 2))
'Column 3 variable
Sheets("Output").Range(target).Value = y.Value
x(0, 0) = Sheets("Data").Cells(Item.Row, Item.Offset(0, 2).Column).Value
Sheets("Output").Activate
Range("BS5:BS6") = x
Sheets("XML").Range("XML[#All]").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("BS5:BS6"), CopyToRange:=Range(target), Unique:=False
Next Item
Application.ScreenUpdating = True
End Sub