Copy from one worksheet to another, aggregate from many lines to one based on value in column

Hoppbananen

New Member
Joined
May 4, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi!

So I have this problem I'm stuck on...

I currently have a set of data that's being filled in partly automatic and partly manually, that when user is finished with I'd like to copy to another worksheet and add some information based on the value in column E of the original sheet. Please review my code under:

VBA Code:
Sub CreateImportsheet()
    Dim ows As Worksheet
    Dim tws As Worksheet
    Dim c As Range
    Dim i As Long

    Set ows = Sheets("Data") 'Original sheet
    Set tws = Sheets("Import") 'Target sheet
    i = 3 'this is the first row in the target sheet
    
    With ows
        For Each c In .Range("E2:E412").Cells
              If c.Value = "A" Then
                tws.Range(tws.Cells(i, "A"), tws.Cells(i, "F")).Value = .Range(.Cells(c.Row, "A"), .Cells(c.Row, "F")).Value
                tws.Cells(i, "G") = "6050"
                tws.Cells(i, "H") = tws.Cells(1, 2)
                tws.Cells(i, "I") = tws.Cells(1, 4)
                i = i + 1
              ElseIf c.Value = "B" Then
                tws.Range(tws.Cells(i, "A"), tws.Cells(i, "F")).Value = .Range(.Cells(c.Row, "A"), .Cells(c.Row, "F")).Value
                tws.Cells(i, "G") = "4050"
                tws.Cells(i, "H") = tws.Cells(1, 2)
                tws.Cells(i, "I") = tws.Cells(1, 4)
                i = i + 1
              End If
        Next c
    End With

End Sub

This works fine! However now I have to rebuild it to fit in another format, gonna try and explain with some pictures first:

This is a dummy of my original table:

ID_NameID_NrPostal codeSome info_1Value_XSome info_2
1​
1000​
A
1​
1001​
A
1​
1002​
A
2​
1003​
B
1​
1004​
A
3​
1005​
B
3​
1006​
B
4​
1007​
A
4​
1008​
A
4​
1009​
A
4​
1010​
A


And I want to go trough it and create this:

ID_NameID_NrPostal code_FromPostal code_ToSome info_1Value_XSome info_2Adding info_1Adding info_2Adding info_3
1​
1000​
1002​
A
2​
1003​
1003​
B
1​
1004​
1004​
A
3​
1005​
1006​
B
4​
1007​
1010​
A

Where I essentially go through ID_Nr and create an interval of postal codes if value in ID_Nr isn't broken. First three lines have the same ID_nr, therefore the data exported to new table will be on one line with the same ID_Nr and MIN postal code and MAX postal code (or first or last if you want). Column containing postal codes in original sheet will always be sorted from smallest to largest. I then add info based on the value in Value_X, in addition to some user added input (through a cell reference).


Is there anybody out there that can lead me in the right direction?

BR
-Hoppbananen
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,136,853
Messages
5,678,132
Members
419,746
Latest member
tysonboy82

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
Top