Insert new line each time condition is met in a 2-dim array

Tango_Bravo

New Member
Joined
Jun 14, 2017
Messages
33
[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I have a few tables in one worksheet. All of them have different names. All start at one row, but at different column. In one of the tables I have a 2dimensional array. Array comprises information about documents (call them 'original'), which have been subsequently amended with another document (call them 'amendment').

here is a sample:
[TABLE="width: 420"]
<tbody>[TR]
[TD]Orig doc
[/TD]
[TD]amendment
[/TD]
[TD]Date
[/TD]
[TD]Column4
[/TD]
[TD]Column5
[/TD]
[/TR]
[TR]
[TD]VAL00002540
[/TD]
[TD]VAL00002540
[/TD]
[TD]7.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002540
[/TD]
[TD]VAL00002541
[/TD]
[TD]7.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002564
[/TD]
[TD]27.11.2017
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002696
[/TD]
[TD]27.3.2018
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002697
[/TD]
[TD]27.3.2018
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002566
[/TD]
[TD]30.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002593
[/TD]
[TD]21.12.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002594
[/TD]
[TD]21.12.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Col 1 represents indentification of the original document. Col 2 represents identification of the current document. Col. 3 represents date of the current document. Whenever vc(c, 1) = vc(c, 2) we are looking at original document. Whenever vc(c, 1) <> vc(c, 2) we are looking at amendment. Col 1 helps me keep track the original document. Cols 2 and 3 provide the details of the amendment.

I would like, upon each amendment to insert new line containing the details of the original document. here is an example of what I want to achieve (added row are marked up in red):

[TABLE="width: 420"]
<tbody>[TR]
[TD]Orig Invoice
[/TD]
[TD]Secondary doc
[/TD]
[TD]Date
[/TD]
[TD]Column4
[/TD]
[TD]Column5
[/TD]
[/TR]
[TR]
[TD]VAL00002540
[/TD]
[TD]VAL00002540
[/TD]
[TD]7.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002540
[/TD]
[TD]VAL00002541
[/TD]
[TD]7.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002540
[/TD]
[TD]VAL00002540
[/TD]
[TD]7.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002564
[/TD]
[TD]27.11.2017
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002696
[/TD]
[TD]27.3.2018
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002564
[/TD]
[TD]27.11.2017
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002697
[/TD]
[TD]27.3.2018
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002564
[/TD]
[TD]VAL00002564
[/TD]
[TD]27.11.2017
[/TD]
[TD]
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002566
[/TD]
[TD]30.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002593
[/TD]
[TD]21.12.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002566
[/TD]
[TD]30.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002594
[/TD]
[TD]21.12.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]VAL00002566
[/TD]
[TD]VAL00002566
[/TD]
[TD]30.11.2017
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


this is how far I've reached, and I realise I am doing it totally wrong (it is part of larger code hence).

ReDim vg(1 To UBound(vc, 1), 1 To 10)

For c = 2 To UBound(vc, 1) 'To 2 Step -1
g = g + 1​
If c = 2 Then​
Else​
If vc(c, 1) = vc(c - 1, 1) & vc(c, 2) <> vc(c - 1, 2) Then​
vg(g, 1).EntireRow.Insert​
Else​
vg(g, 1) = vc(c, 1)​
End If​
End if​
Next

Please help!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I have a few tables in one worksheet. All of them have different names. All start at one row, but at different column.
If the tables are adjacent to each other and start in the same row (or have common rows) then doing EntireRow.Insert will affect all the tables and adjacent cells. With Excel tables, using the correct table properties, you can add (insert) rows in one table without affecting other tables, or adjacent cells.

Try this code, which assumes your table is named "Table1". For test purposes, the code also puts "New row" in the 4th column to indicate where a row has been inserted.

Code:
Public Sub Add_Row_Each_Amendment()

    Dim amendmentTable As ListObject
    Dim r As Long, i As Long
    Dim newRow As ListRow
    
    Set amendmentTable = ActiveSheet.ListObjects("Table1")
    
    With amendmentTable
        For r = .DataBodyRange.Rows.Count To 1 Step -1
            If .ListRows(r).Range(1).Value <> .ListRows(r).Range(2).Value Then
                'Add new row below
                Set newRow = .ListRows.Add(r + 1)
                'Find original document row in rows above
                i = r
                Do
                    i = i - 1
                Loop While i > 1 And .ListRows(i).Range(1).Value <> .ListRows(i).Range(2).Value
                'Copy original document row to new row
                newRow.Range(1).Value = .ListRows(i).Range(1).Value
                newRow.Range(2).Value = .ListRows(i).Range(1).Value
                newRow.Range(3).Value = .ListRows(i).Range(3).Value
                newRow.Range(4).Value = "New row"
            End If
        Next
    End With
       
End Sub
 
Upvote 0
Thank you very much, John_w!

It worked perfect! I made some adjustment to your code to match my scenario.

But now I am stuck with the next and final step in the process.

The table that you helped generate represented range of original documents (invoices), amendments (credit notes) and final documents (final invoices). Thanks to you the original document appears after each amendment and final document.

In my scenario each original document aggregates 1 to x number of records with values (sometimes different sometimes not). Each amendment and each final document comprise one record only each one of them. There is a separate table which keeps track of these records, their values and the document to which the records and values are subscribed. Here is an example:


[TABLE="class: grid"]
<tbody>[TR]
[TD]Col1

[/TD]
[TD]Col2
[/TD]
[TD]col3 Record
[/TD]
[TD]col4 Value
[/TD]
[TD]col5
[/TD]
[TD]col6
[/TD]
[TD]col7
[/TD]
[TD]col8Document
[/TD]
[TD]col9
[/TD]
[TD]col10
[/TD]
[TD]col11
[/TD]
[TD]col12
[/TD]
[/TR]
[TR]
[TD]Name1
[/TD]
[TD][/TD]
[TD]1.5
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002540
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name2
[/TD]
[TD][/TD]
[TD]1.0
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002564
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name3
[/TD]
[TD][/TD]
[TD]2.3
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002540
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name2
[/TD]
[TD][/TD]
[TD]2.1
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002540
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name4
[/TD]
[TD][/TD]
[TD]0.5
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002541
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name1
[/TD]
[TD][/TD]
[TD]6.3
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002564
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name4
[/TD]
[TD][/TD]
[TD]2.1
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002564
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name3
[/TD]
[TD][/TD]
[TD]0.1
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002696
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name1
[/TD]
[TD][/TD]
[TD]0.5
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]VAL00002697
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I am trying to generate new 2d array combining the array for which you helped (addressed as 've') and the array (addressed as 'vf') containing the records, their values and the document, to which they are subscribed.

Original document appears 2-3 times in 've', so I need to 'call' 2 or 3 times from 'vf' the records and their values, which are subscribed to each original document. Save for some exceptions, records and their values behind original document are "called" 3 times: 1) for original document; 2) after an amendent; and 3) after a final document.

I do not know to how call multiple times records from one and the same array. It seems ReDim Preserve might be useful, but I do not know its syntax, because this is all very new to me and I am not a developer. Here is the code so far, but without my the lamer's attempts on ReDim Preserve. One thing for certain is I am not doing ReDim Preserve:
Code:
ReDim vg(1 To UBound(vf, 1), 1 To 20)

For f = 1 To UBound(vf, 1)
    g = g + 1
    For e = 1 To UBound(ve, 1)
        If vf(f, 8) = ve(e, 2) Then
            vg(g, 1) = ve(e, 1) 'original document
            vg(g, 2) = vf(f, 8) 'current document
            vg(g, 12) = vf(f, 3)    'recorded time
        End If
    Next
Next
 
Upvote 0

Forum statistics

Threads
1,224,971
Messages
6,182,090
Members
453,088
Latest member
Chaoxite

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top