Hi
Here’s another way. It probably isn’t any better. Just another way out of interests for comparison:
The shortened TLDR version of the Story:
We know that we can build up a single string, which we can put into the Windows Clipboard, and then
.Paste out into Excel, such that it makes a typical Excel range of values (
http://www.eileenslounge.com/viewtopic. ... 41#p242941 )
( So like in a long string of the range values, we can include a
vbTab, which can be thought of as a “cell wall”, and including something like
vbCr & vbLf is what seems to separate rows. )
In other words: This is the final wanted output:
ThatWantedHorizontal.JPG
So to get that in the clipboard in a form to
.Paste out, I need to build up a string looking pseudo like
Code: Select all
"A" & vbTab & "10" & vbTab & "20" & vbTab & "30" & vbTab & vbTab & "N1" & vbTab & "N2" & vbTab & "N3" & vbTab & vbTab & "GroupA" & vbCr & vbLf
"B" & vbTab & "40" & vbTab & "50" & vbTab & "60" & vbTab & "70" & vbTab & "N4" & vbTab & "N5" & vbTab & "N6" & vbTab & "N7" & vbTab & "GroupB" & vbCr & vbLf
"C" & vbTab & "80" & vbTab & vbTab & vbTab & vbTab & "N8" & vbTab & vbTab & vbTab & vbTab & "GroupC" & vbCr & vbLf
"D" & vbTab & "90" & vbTab & "100" & vbTab & vbTab & vbTab & "N9" & vbTab & "N10" & vbTab & vbTab & vbTab & "GroupD" & vbCr & vbLf
So basically what I am doing is using some
Do While Loopy stuff applied to the input data range, to produce that final required output data range string.
I add to that string what I need to do something similar to get the header row.
The total final string is then put in the Windows clipboard and pasted out.
This following macro seems to work on the supplied test data
Code: Select all
'
Sub TFLDFR() ' http://www.eileenslounge.com/viewtopic.php?p=294692#p294692
Dim Ws1 As Worksheet: Set Ws1 = ThisWorkbook.Worksheets("Sheet1")
Dim RngPlus1 As Range
Set RngPlus1 = Ws1.Cells.Item(1).CurrentRegion.Resize(Ws1.Cells.Item(1).CurrentRegion.Rows.Count + 1, Ws1.Cells.Item(1).CurrentRegion.Columns.Count)
Dim vArr() As Variant: Let vArr() = RngPlus1.Value2
' determine the biggest group ( that maximum Amounts or Notes count )
Dim Cnt As Long, Cnt2 As Long, Mx As Long: Let Mx = 1: Let Cnt = 1
Do
Do
Let Cnt = Cnt + 1
Let Cnt2 = Cnt2 + 1
Loop While vArr(Cnt + 1, 1) = vArr(Cnt, 1)
If Cnt2 > Mx Then Let Mx = Cnt2
Let Cnt2 = 0
Loop While Cnt < UBound(vArr(), 1) - 1
' Main data range string reqiured for clipboard
Let Cnt = 1
Do
Dim HrCnt As Long: Let HrCnt = 1
Dim strClipR As String, strClipL As String: Let strClipL = strClipL & vArr(Cnt + 1, 1)
Do
Let Cnt = Cnt + 1
Let HrCnt = HrCnt + 1
Let strClipL = strClipL & vbTab & vArr(Cnt, 2)
Let strClipR = strClipR & vbTab & vArr(Cnt, 3)
Loop While vArr(Cnt + 1, 1) = vArr(Cnt, 1)
Do While HrCnt < Mx + 1
Let strClipL = strClipL & vbTab
Let strClipR = strClipR & vbTab
Let HrCnt = HrCnt + 1
Loop
Let strClipR = strClipR & vbTab & vArr(Cnt, 4)
Dim strClip As String: Let strClip = strClip & strClipL & strClipR & vbCr & vbLf
Let strClipL = "": strClipR = ""
Loop While Cnt < UBound(vArr(), 1) - 1
' header string required for clipboard
Dim strHd As String
Let strHd = "Group" & vbTab & Join(Evaluate("=""Amount"" & COLUMN(A:" & Split(Cells(1, Mx).Address, "$")(1) & ")"), vbTab) & vbTab & Join(Evaluate("=""Note"" & COLUMN(A:" & Split(Cells(1, Mx).Address, "$")(1) & ")"), vbTab) & vbTab & "Name"
' full string for clipboard and paste out
Let strClip = strHd & vbCr & vbLf & strClip
Dim objDataObject As Object: Set objDataObject = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
objDataObject.SetText Text:=strClip
objDataObject.PutInClipboard
Ws1.Paste Destination:=Ws1.Range("G1")
End Sub
_.____________________________
If you are interested in the full boring story, I put it here:
https://excelfox.com/forum/showthread.p ... #post16529
https://excelfox.com/forum/showthread.p ... #post16530
https://excelfox.com/forum/showthread.p ... #post16532
https://excelfox.com/forum/showthread.p ... #post16533
Alan
_.____________________________________________
ThatHorizontal.xlsm :
https://app.box.com/s/aimwwh88rqgsl8cawyhwcuyz5ebhjb6z
You do not have the required permissions to view the files attached to this post.