Need code to reconstruct a list from raw data

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Need code to reconstruct a list from raw data

Post by Rudi »

Hi,

I would normally try to code this myself but lots to do today, so if anyone has some time to spare I'd appreciate the help. :cheers:
Please see attached to clarify.

Many Thanks
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Need code to reconstruct a list from raw data

Post by Rudi »

I have found time to set up a macro, but it probably could be optimised since it will be run on a regular basis..
Also the addresses start and end with commas...

Any optimization and improvements will be appreciated.

TX
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

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

Re: Need code to reconstruct a list from raw data

Post by HansV »

Nice solution!

One small improvement: you don't have to repeat computing Range("D" & Rows.Count).End(xlUp) - you only need to increase the row when you encounter "Acc".
The following version also removes the leading ", ":

Code: Select all

Sub MakeList()
    Dim rData As Range
    Dim rC As Range
    Dim i As Integer
    Dim sAddr As String
    Dim r As Long
    r = 1
    Set rData = Range("A1").CurrentRegion
    Application.ScreenUpdating = False
    For Each rC In rData.Columns(1).Cells
        If rC.Value = "Acc" Then
            r = r + 1
            rC.Offset(0, 1).Copy Range("D" & r)
        End If
        If rC.Value = "Name" Then
            rC.Offset(0, 1).Copy Range("E" & r)
        End If
        If rC.Value = "Delivery Address" Then
            For i = 1 To 10
                If rC.Offset(i, 0).Value = "Acc" Then
                    Exit For
                End If
                If rC.Offset(i, 0).Value <> "" Then
                    sAddr = sAddr & ", " & rC.Offset(i, 0).Value
                End If
            Next i
            Range("F" & r) = Mid(sAddr, 3)
        End If
        sAddr = ""
        If rC.Offset(0, 1).Value = "Postal Address" Then
            For i = 1 To 10
                If rC.Offset(i, 0).Value = "Acc" Then
                    Exit For
                End If
                If rC.Offset(i, 1).Value <> "" Then
                    sAddr = sAddr & ", " & rC.Offset(i, 1).Value
                End If
            Next i
            Range("G" & r) = Mid(sAddr, 3)
        End If
        sAddr = ""
    Next rC
    Application.ScreenUpdating = True
    MsgBox "Data compiled.", vbInformation
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Need code to reconstruct a list from raw data

Post by Rudi »

HansV wrote:Nice solution!
Wow...that's a BIG compliment coming from you.
I have mind to screenprint this page, print it and frame it on my wall. :grin:

Many TX for taking the time to review the code and add improvements. I'll scrutinize your changes and take notes.

Cheers :chocciebar:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.