Sub CopyData(rij As Long)
'********************************************************************************************
bquick = False '--------------> MAKE THIS VARIABLE TRUE(=TESTING) OR FALSE(NORMAL)
'********************************************************************************************
If Not bquick Then MsgBox "new macro" 'to be sure you call this new macro
t = Timer
Set sh2 = Sheets("Summary") 'your sheet2
Set sh6 = Sheets("Archived Employee Data") 'your sheet6
Do
ptr = ptr + 1 'a pointer to know how many loops
btest = False 'flag no columns added
a = sh2.Range("A1").CurrentRegion.Resize(3) 'read 1st 2 rows of sheet2 = header + 1st datarow
b = sh6.Range("A1").CurrentRegion.Resize(1) 'read 1 row of sheet6 = header
If Not bquick Then MsgBox "at start loop : " & ptr & vbLf & sh2.Name & " and " & sh6.Name & " have " & UBound(a, 2) & " and " & UBound(b, 2) & " columns"
On Error GoTo fout
For k = UBound(a, 2) To 1 Step -1 'loop from last column to 1st column of sheet2
k1 = Application.Match(a(1, k), b, 0) 'check if header also exists in sheet6
If Not IsNumeric(k1) Then 'if not
sh6.Range("E1").EntireColumn.Insert 'insert new column in E
sh6.Range("E1").Value = a(1, k) 'new headername
btest = True 'flag new column added
k1 = "new" 'k1 is not numeric, an error, so make it a string instead to avoid that error
End If
a(2, k) = k1 'replace the 2nd row with the corresponding column in sheet6
a(3, k) = a(1, k) & " " & a(2, k)
Next
On Error GoTo 0
GoTo okay
fout:
MsgBox "k is " & k: End
okay:
If Not bquick Then
MsgBox "columns " & sh2.Name & vbLf & "name and columnnumber" & vbLf & vbLf & Join(Application.Index(a, 3, 0), vbLf) & IIf(btest, vbLf & vbLf & "because of new added column(s), a 2nd run", "")
Application.Goto sh6.Range("A1")
MsgBox "columns " & sh6.Name & vbLf & vbLf & Join(Application.Transpose(Application.Transpose(b)), vbLf)
End If
Loop While btest = True And ptr < 3 'normally 1 (no columns inserted) or 2 loops (at least 1 column inserted
If ptr >= 3 Then MsgBox "inpossible, ptr can never be >=3": Exit Sub
imax = Application.Max(Application.Index(a, 2, 0)) 'the max corresponding column number in sheet6
arr = sh2.UsedRange.Resize(, UBound(a, 2)).Value 'read data into an array
ReDim arr1(1 To imax) 'clear aux. array
For k = 1 To UBound(arr, 2) 'loop from 1st to last column of sheet2
arr1(a(2, k)) = arr(rij, k) 'fill aux. array in the right element
Next
sh6.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, imax).Value = arr1 ' Application.Index(dict.items, 0) 'if there is data, add it to sheet6
Application.EnableEvents = False
sh2.Rows(rij).Delete 'you want to delete that row ????
Application.EnableEvents = True
MsgBox "transferred in " & Format(Timer - t, "0.00") & " sec"
End Sub