position reporting

sachin483
2StarLounger
Posts: 101
Joined: 03 Feb 2018, 04:20

position reporting

Post by sachin483 »

I have position code and reporting position and in 2 column, but I want the format of reporting one below another, i.e.: 4 level will report to 3 and 3 level report to 2 and 2 level will report to 1. If any level is not there, then create blank level for upper position. Example in attached file.
You do not have the required permissions to view the files attached to this post.
Last edited by HansV on 22 Apr 2023, 08:56, edited 1 time in total.
Reason: to correct spelling errors

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: postion reporting

Post by HansV »

If you have Microsoft 365, it is probably possible to use a LAMBDA function, but in the attached workbook, I have used a macro.
The result is equivalent to yours but the order of the rows at the same level is not exactly the same.

report.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

sachin483
2StarLounger
Posts: 101
Joined: 03 Feb 2018, 04:20

Re: postion reporting

Post by sachin483 »

Thanks working fine but takes time for more than 20000 rows and kindly show the Lamda function in o365

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: postion reporting

Post by HansV »

I cannot help you with that; I hope that someone else will.
Best wishes,
Hans

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: postion reporting

Post by snb »

Probably faster alternative:

Code: Select all

Sub M_snb()
  sn = Cells(1).CurrentRegion
  
  With CreateObject("scripting.dictionary")
    For j = 2 To UBound(sn)
      If sn(j, 4) = 3 Then .Item(sn(j, 2)) = Join(Application.Index(sn, j), "_")
      If sn(j, 4) = 3 And n = "" Then n = j
      If sn(j, 4) = 4 Then .Item(sn(j, 3)) = .Item(sn(j, 3)) & vbLf & Join(Application.Index(sn, j), "_")
    Next
    
    For Each it In .keys
      st = Split(.Item(it), vbLf)
      For j = 0 To UBound(st)
        sq = Split(st(j), "_")
        sn(n, 3) = sq(1)
        sn(n, 4) = sq(3)
        sn(n, 5) = sq(4)
        sn(n, 6) = sq(5)
        n = n + 1
      Next
    Next
  End With
  
  Cells(1, 8).Resize(UBound(sn), UBound(sn, 2)) = sn
End Sub
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: postion reporting

Post by HansV »

If you don't get an Excel 365-specific reply here, try asking your question on Microsoft Excel Community. There are many users there who are very good at the new dynamic array formulas. (Please mention that you're cross-posting)
Best wishes,
Hans

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: postion reporting

Post by DocAElstein »

Hi
sachin483 wrote:
19 Apr 2023, 12:33
...working fine but takes time for more than 20000 rows ....
I can’t help with a different solution as I haven’t been able to figure out yet what is wanted or what the solutions from Hans and snb are doing, :( :-( ( – That’s my problem / disability, in needing usually 10-100 times longer than everyone else to get the point, Lol. I would likely get it in a few more hours , ( or days usually trying to figure out what snb’s coding does, Lol) )

But assuming Hans coding is doing what you want, you could try this very minor modification. I doubt it will make a significant improvement in the speed of the macro, but it might give a small improvement. So it’s possibly worth a quick try comparing the modified version of mine below, and the original unmodified version from Hans.
(If you do that comparison, you would be best to try the experiment a few times and average the results, since the final speeds can be a bit variable depending on all sorts of things that might or might not be going on at any particular time)

Code: Select all

Option Explicit
Const SourceDivCol = 1
Const SourcePosCol = 2
Const SourceRepCol = 3
Const SourceLevCol = 4
Const SourceEmpCol = 5
Const SourceCodCol = 6
Const TargetDivCol = 15
Const TargetLevCol = 16
Const TargetPosCol = 17
Const TargetEmpCol = 18
Const TargetCodCol = 19
Dim SourceRow As Long
Dim TargetRow As Long
' http://www.eileenslounge.com/viewtopic.php?p=306780#p306780
' i have postion code and reporting postion and in 2 column but i want the format of reporting one below another  ie :- 4 level will report to 3 and 3 level report to 2 and 2 level will report to 1  if any level is not there then create blank level for upper postion example in attached file
Sub CreateReport() ' http://www.eileenslounge.com/viewtopic.php?p=306785#p306785 (Hans)
    Dim Boss As Range
    Dim Adr As String
    Dim Pos As String
    Application.ScreenUpdating = False
    TargetRow = 2
    Set Boss = Columns(SourceLevCol).Find(What:=1, LookAt:=xlWhole)
    Adr = Boss.Address
    Do
        SourceRow = Boss.Row
        TargetRow = TargetRow + 1
     
'     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(1, 4, 2, 5, 6))
     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
'        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
'        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
'        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
'        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
        Pos = Cells(SourceRow, SourcePosCol).Value
        Call AddChildren(Pos)
        Set Boss = Columns(SourceLevCol).Find(What:=1, After:=Boss, LookAt:=xlWhole)
        If Boss Is Nothing Then Exit Do
    Loop Until Boss.Address = Adr
    Application.ScreenUpdating = True
End Sub

Sub AddChildren(BossPos As String) ' http://www.eileenslounge.com/viewtopic.php?p=306785#p306785 (Hans)
    Dim Child As Range
    Dim Adr As String
    Dim Pos As String
    Set Child = Columns(SourceRepCol).Find(What:=BossPos, LookAt:=xlWhole)
    If Child Is Nothing Then Exit Sub
    Adr = Child.Address
    Do
        SourceRow = Child.Row
        TargetRow = TargetRow + 1
     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
'        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
'        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
'        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
'        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
        Pos = Cells(SourceRow, SourcePosCol).Value
        Call AddChildren(Pos)
        Set Child = Columns(SourceRepCol).Find(What:=BossPos, After:=Child, LookAt:=xlWhole)
        If Child Is Nothing Then Exit Do
    Loop Until Child.Address = Adr
End Sub


Alan
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: postion reporting

Post by snb »

@Doc

To speed up code you should reduce the interaction with the workbook to 2 instances: reading data, writing the result.
The rest should be performed in memory.

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: postion reporting

Post by DocAElstein »

Yep, I think I generally agree with that, - that often seems to be the best way.
My modification is not doing that exactly, but it is doing something similar in that it's reducing the number of interactions with the spreadsheet, I think....
If I understand correctly, This bit ....._

Code: Select all

     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
'        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
'        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
'        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
'        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
_... reduces those original 5 interactions to 1
But I don't understand enough about the full coding to know if , in this case, it makes a significant improvement in speed performance...
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: postion reporting

Post by snb »

The crux in the question
Change the order of items from 1,2,3,3,3,4,4,4,4,4,4,4,4,4
to inserting the '4' items after the '3' item it belongs to (where cells(n,3) matches cells(y,2))
Resulting order: 1,2,3,4,4,4,3,4,4,4,3,4,4,4

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: postion reporting

Post by snb »

If you like a simple approach, use autofilter
You do not have the required permissions to view the files attached to this post.

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: postion reporting

Post by DocAElstein »

snb wrote:
21 Apr 2023, 09:22
....crux the question..Change the order of items from 1,2,3...4,4,4,3,4,4,4
OK, thanks,- with that and looking again at the original OP stuff, with colours, https://bit.ly/40tChKH , I think I am getting closer to understand what is wanted, although I am still not quite sure what this means… if any level is not there then create blank level for upper postion

As I am still not sure what is wanted exactly I will just do another version of Hans original macro, that might be a bit quicker, using something we stumbled across here
https://eileenslounge.com/viewtopic.php ... 91#p266691

This is the main new thing going on in this version….
If we have a 1 D array of arrays , arr1D(), like for example a 2 element 1 D array with each element itself a 2 element 1 D array, like this,
{ {"a", "b"} , { "c", "d" } }
, then strangely, that array acts in this sort of code line,
arrOut()=App.Index(arr1D(), Rws(), Clms())
, just as if it was a 2 x 2 2D array like this
{"a", "b"
"c", "d" }


Strange, but true.

So in Hans macro from here,
http://www.eileenslounge.com/viewtopic. ... 85#p306785
, or rather the modified one from here ,
http://www.eileenslounge.com/viewtopic. ... 80#p306880
, instead of
_ pasting a 1 D array out each time, ( so effectively pasting out a line each time),
_ we alternatively now add that array to an array of arrays, then finally paste out that final array using the arrOut()=App.Index(arr1D(), Rws(), Clms()) code line.

It is still not a solution like the ideal …. ,
reduce the interaction with the workbook to 2 instances: reading data, writing the result
, … but it's getting closer. ( A remaining interactions which probably slow the macro down a bit is the Range.Find bits. )

Code: Select all

 Option Explicit
Const SourceDivCol = 1
Const SourcePosCol = 2
Const SourceRepCol = 3
Const SourceLevCol = 4
Const SourceEmpCol = 5
Const SourceCodCol = 6
Const TargetDivCol = 15
Const TargetLevCol = 16
Const TargetPosCol = 17
Const TargetEmpCol = 18
Const TargetCodCol = 19
Dim SourceRow As Long
Dim TargetRow As Long
Dim Cnt As Long
Dim WunDeeArrayOfArrays() As Variant



Sub CreateReportHansAlan2() '
 ReDim WunDeeArrayOfArrays(1 To Cells(1).CurrentRegion.Rows.Count - 2)
    Dim Boss As Range
    Dim Adr As String
    Dim Pos As String
    Application.ScreenUpdating = False
    TargetRow = 2
    Set Boss = Columns(SourceLevCol).Find(What:=1, LookAt:=xlWhole)
    Adr = Boss.Address
    Do
        SourceRow = Boss.Row
        TargetRow = TargetRow + 1
     Let Cnt = Cnt + 1: Let WunDeeArrayOfArrays(Cnt) = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(1, 4, 2, 5, 6))
'     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
'        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
'        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
'        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
'        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
        Pos = Cells(SourceRow, SourcePosCol).Value
        Call AddKids(Pos)
        Set Boss = Columns(SourceLevCol).Find(What:=1, After:=Boss, LookAt:=xlWhole)
        If Boss Is Nothing Then Exit Do
    Loop Until Boss.Address = Adr
    Application.ScreenUpdating = True
 
 Let Range("O3").Resize(Cells(1).CurrentRegion.Rows.Count - 2, 5).Value2 = Application.Index(WunDeeArrayOfArrays, Evaluate("=ROW(1:" & Cells(1).CurrentRegion.Rows.Count - 2 & ")"), Evaluate("=COLUMN(A:E)"))
End Sub

Sub AddKids(BossPos As String) '
    Dim Child As Range
    Dim Adr As String
    Dim Pos As String
    Set Child = Columns(SourceRepCol).Find(What:=BossPos, LookAt:=xlWhole)
    If Child Is Nothing Then Exit Sub
    Adr = Child.Address
    Do
        SourceRow = Child.Row
        TargetRow = TargetRow + 1
     Let Cnt = Cnt + 1: Let WunDeeArrayOfArrays(Cnt) = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'     Let Range("O" & TargetRow & ":S" & TargetRow & "").Value2 = Application.Index(Cells, SourceRow, Array(SourceDivCol, SourceLevCol, SourcePosCol, SourceEmpCol, SourceCodCol))
'        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
'        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
'        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
'        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
'        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
        Pos = Cells(SourceRow, SourcePosCol).Value
        Call AddKids(Pos)
        Set Child = Columns(SourceRepCol).Find(What:=BossPos, After:=Child, LookAt:=xlWhole)
        If Child Is Nothing Then Exit Do
    Loop Until Child.Address = Adr
End Sub

Just to help avoid confusion on the three different forms so far that are similar, ( versions of Hans macro ) in case anyone wants to compare the speed performance of them, in the workbook enclosed to this post, we have

Sub CreateReport() - that is Hans original ( http://www.eileenslounge.com/viewtopic. ... 85#p306785 )

Sub CreateReportHansAlan1() that‘s the first modification ( http://www.eileenslounge.com/viewtopic. ... 80#p306880
http://www.eileenslounge.com/viewtopic. ... 83#p306883
) What is going on there is reducing a 5 line section pasting out 5 cells with a single line paste out of all 5 cells ( a row ) in one go


Sub CreateReportHansAlan2() - That is the one that is being discussed in this post that we are in at the moment , ( https://eileenslounge.com/viewtopic.php ... 12#p306912 )




Alan


Ref:
https://bit.ly/41w3ldC
https://bit.ly/3mVfyJT
https://www.ozgrid.com/forum/index.php? ... ost1239241
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: position reporting

Post by snb »

Alternatively you could use a Treeview in Memory:
Reding once, writing once.

Code: Select all

Sub M_snb()
  sn = Sheet1.Cells(1).CurrentRegion
  
  With GetObject("New:{9181DC5F-E07D-418A-ACA6-8EEA1ECB8E9E}")
    For j = 2 To UBound(sn)
      If sn(j, 4) < 4 Then
        .Nodes.Add , , sn(j, 2), Join(Application.Index(sn, j), "_")
      Else
        .Nodes.Add sn(j, 3), 4, sn(j, 2), Join(Application.Index(sn, j), "_")
      End If
    Next
    
    n = 2
    For j = 1 To .Nodes.Count
      If n > UBound(sn) Then Exit For
      st = Split(.Nodes(j).Text, "_")
      For jj = 0 To UBound(st)
        sn(n, jj + 1) = st(jj)
      Next
      n = n + 1
      For m = 1 To .Nodes(j).Children
        If m = 1 Then Set nc = .Nodes(j).Child
        If m > 1 Then Set nc = nc.Next
        st = Split(nc.Text, "_")
        For jj = 0 To UBound(st)
          sn(n, jj + 1) = st(jj)
        Next
        n = n + 1
      Next
    Next
  End With
  
  Sheet1.Cells(1, 20).Resize(UBound(sn), UBound(sn, 2)) = sn
End Sub

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: position reporting

Post by DocAElstein »

Hello,
Here is a another solution, - it is simpler, but , it’s a restricted solution….. To explain that restrictions …
...... I missed the point, ( possibly ), with the OPs original data, saying he had like this
1,2, 3 , 3 , 3 , 4 , 4 , 4 , 4, 4 , 4 , 4 . 4 , 4 , 4 , 4 , 4
, but wanted this:
1,2, 3 , 4 , 4 . 4 , 4 , 3 , 4 , 4 , 4 , 4 , 4 , 3 , 4 , 4 , 4
I missed the point ( possibly ) that there could be more than one level 2 and that maybe the levels could go on a lot further down/ above level 4. Maybe that additional information is obvious to most people? It is not to me. The more flexible open ended requirement would explain all the recursioning, explorer tree view type things discussed.

Never mind.. , a restricted scenario could still be useful to investigate for another solution.
Restricted solution
Restrictions:

One Big Boss , level 1
, one deputy ( who does all the big Boss’s work, Lol ) , Level 2
, or rather the deputy organises the line managers, level 3
, and those line managers in turn have all the workers organised beneath them, level 4. ( Level 4 in the restricted solution being the workers actually doing everything of any use, Lol…. )

Here is a simplified version :

Code: Select all

 Sub AlanReporting() '    https://excelfox.com/forum/showthread.php/2837-Appendix-Thread-App-Index-Rws()-Clms()-Majic-code-line-Codings-for-other-Threads-Tables-etc-)?p=19941&viewfull=1#post19941
Dim Ws1 As Worksheet: Set Ws1 = ThisWorkbook.Worksheets.Item(1)
Dim arrIn() As Variant: Let arrIn() = Ws1.Range("A1").CurrentRegion.Value2
Dim arr1DArrays() As Variant: ReDim arr1DArrays(1 To UBound(arrIn(), 1)) '
Dim Lr As Long: Let Lr = UBound(arrIn(), 1)
Rem 1 some initial lines in the final output, based on the   Restrictions  of  one Boss and 1 deputy, so in other words one level 1 and one level 2
 Let arr1DArrays(1) = Application.Index(Ws1.Cells, 1, Array(1, 4, 2, 5, 6)): arr1DArrays(2) = Application.Index(Ws1.Cells, 2, Array(1, 4, 2, 5, 6)): arr1DArrays(3) = Application.Index(Ws1.Cells, 3, Array(1, 4, 2, 5, 6)): arr1DArrays(4) = Application.Index(Ws1.Cells, 4, Array(1, 4, 2, 5, 6))
Rem 2a
Dim Dw As Long: Let Dw = 4
Dim srchVl As String: Let srchVl = arrIn(Dw, 2)
Dim arrInds3() As Variant: Let arrInds3() = Ws1.Evaluate("=IF(C5:C" & Lr & "=$B$4,ROW(B5:B" & Lr & "),0)")
Rem 3b
Dim Inds3 As Long
    For Inds3 = 1 To UBound(arrInds3(), 1)
     If arrInds3(Inds3, 1) = 0 Then: Dim Lvl3s As Long: Let Lvl3s = Inds3 - 1: Exit For
    Next Inds3
Rem 4a
'  now we want to investigate all the level 4s reporting to all the level 3s
Dim CntInds3 As Long ' Outer loop, Looping all level 3s ' ===================================================
    For CntInds3 = 1 To Lvl3s ' Looping all level 3s
     Let Dw = Dw + 1
     Let arr1DArrays(Dw) = Application.Index(Ws1.Cells, 5 + CntInds3 - 1, Array(1, 4, 2, 5, 6))
    Rem 4b
    Dim arrInds4() As Variant: Let arrInds4() = Ws1.Evaluate("=IF(C" & 5 + Lvl3s & ":C" & Lr & "=$B$" & 5 + CntInds3 - 1 & ",ROW(C" & 5 + Lvl3s & ":C" & Lr & "),0)")
    Rem 4c
    Dim CntInds4s As Long ' Inner loop, Looping all level 4s for a level 3 ' --------------------------------
        For CntInds4s = 1 To UBound(arrInds4(), 1)
            If arrInds4(CntInds4s, 1) = 0 Then
            
            Else
             Let Dw = Dw + 1 '
             Let arr1DArrays(Dw) = Application.Index(Ws1.Cells, arrInds4(CntInds4s, 1), Array(1, 4, 2, 5, 6))
            End If
        Next CntInds4s ' ------------------------------------------------------------------------------------
    Next CntInds3 ' =========================================================================================
Rem 5 Output - convert the 1D array of 1D array output rows to a 2D range form
 Let Range("AE1").Resize(Lr, 5).Value2 = Application.Index(arr1DArrays(), Evaluate("=ROW(1:" & Lr & ")"), Evaluate("=COLUMN(A:E)"))
End Sub

A fuller version along with some more wordy explanations is here:
https://excelfox.com/forum/showthread.p ... ables-etc-)?p=19941&viewfull=1#post19941
https://bit.ly/3HaYOFw


We note a slight difference in order presented in the final results for the level 4s,
https://excelfox.com/forum/showthread.p ... ables-etc-)?p=19943&viewfull=1#post19943
https://bit.ly/3LpFarN
, when compared with results from the other solutions so far given
https://excelfox.com/forum/showthread.p ... ables-etc-)?p=19944&viewfull=1#post19944
https://bit.ly/3L5bLBV

- This sort of difference is commonly seen when comparing
_ explorer / recursioning type solutions
, with
_ simpler looping ones which build up the results one line after the other
This difference is explained in that recursioning type solutions go up and down the explorer tree view structure thingy and so often order the final results a bit differently - the final results get built up in recursioning type solutions in a less ordered , more difficult to visualise and predict way. ( Recursion type solutions can be a nice way to obfuscate coding. ( Not saying that is the case at all here. Just a general comment, that’s all ) )


Alan
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: position reporting

Post by snb »

@Doc

In the file a solution applying a 'virtual' Treeview and recursion to transform the Treeview data into an array.
It also complies to the 'reading once, writing once' requirement.
You do not have the required permissions to view the files attached to this post.

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: position reporting

Post by DocAElstein »

That seems to work and looks interesting. I put it on my list of things to come back and look at after I have discovered the secret to eternal life: I usually need close to infinite time to decipher your codings.
It would be interesting to see something similar using a Do While type structure as a comparison to such a recursion type solution. - I can’t comment on this particuklar example, until I have discovered the secret to eternal life, but I did try looking at some other famous recursion solutions and did a Do While alternative. The Do While worked similarly, sometimes a bit better.
Recursion solutions can be a bit misleading , JIMVHO, in that they look more compact and efficient then they are. I think, once again, JIMVHO, it is partly due to them falsely said to be to do with functions calling themselves. Maybe that is sort of technically correct, but you actually run a whole lot of copies of similar functions, but just with the difference that you start running another while one or more previous ones are not finished yet, so they get paused whilst you are running another one. If you actually wrote out the full coding being used in a typical run, then it would be quite long, similar to if you wrote out the full coding done in a looping process.
The recursion coding looks perhaps a bit cleverer, or is more difficult to understand, which often amounts to the same thing I suppose.
Maybe there are some occasions when recursion might be the only efficient way. But my gut feeling is that in a lot of cases it’s just because no one tried to come up with a simpler alternative that worked as well. But I am not sure about that.

I personally would put recursion alongside using Class things in VBA as useful tools for code obfuscation, but once again, JIMVHO, and not necessarily in this particular example
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: position reporting

Post by snb »

@Doc

If you prefer simple solutions: use sort.

Code: Select all

Sub M_snb()
  sn = Sheet1.Cells(1).CurrentRegion
  For j = 2 To UBound(sn)
    If sn(j, 3) = 4 Then sn(j, 1) = sn(j, 2) & "_" & sn(j, 1)
    If sn(j, 3) = 3 And n = "" Then n = j - 1
  Next
   
  With Sheet1.Cells(24, 1)
    .Resize(UBound(sn), UBound(sn, 2)) = sn
    .CurrentRegion.Offset(n).Sort .Offset(n)
  End With
End Sub

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: position reporting

Post by DocAElstein »

Yeh, I expect something like this can be done in lots of different ways in VBA.

I would not say that I always prefer simple solutions. I do find simple solutions easier to understand.
But the point is that I personally prefer not to use solutions that I do not understand. That’s all.
Just a personal preference: I am already personally getting well fed up and messed up by artificial Intelligence, Chat GPT and co. Just a personal preference I like to understand what’s going on, and I don’t personally like to add to the confusion by using or passing on coding that I don’t understand or can’t explain.
I am not criticising necessarily, I personally think if you are providing free help then you should have some leeway in how you choose to do it.
But I do know that things like Chat GPT were partially developed, and feed off, and learn from, a lot of the simple short answers without explanations given in forums. Most of the most proliferate answering forum members are digging their own virtual grave as it’s a pretty sure thing that most forums will be made read only and / or replaced by Chat GPT soon. Once again I am not criticising, just saying, that’s all. Could be healthy, answering in forums can be a bit addictive, and I think there will be a lot of forum helpers on a cold turkey in the next few years.
Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(