Link to home
Create AccountLog in
Avatar of Stephen Manderson
Stephen MandersonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help speeding up a loop

Hi All

The following loop that loops through and generates all possible postcodes in the UK.

How can I change the attached code so that the outer part of each loop is not repeated?

Ie.

The loop has 4 parts...

The outer loop goes through a list of districts....
             
     The Next part generates (1) 1 Digit
         The inner part will Generate 1 Digit followed by 2 Characters

     The Next part generates (2) 2 Digits
         The inner part will Generate 1 Digit followed by 2 Characters

     The Next part generates (3) 1 Digit & 1 Character
         The inner part will Generate 1 Digit followed by 2 Characters


How would I remove the repeating outer part from each loop?

So that I would only require 1 loop that does something along the lines of

The outer loop goes through a list of districts....
             The Next part generates (1) 1 Digit
             The Next part generates (2) 2 Digits
             The Next part generates (3) 1 Digit & 1 Character
                          The inner will Generate 1 Digit followed by 2 Characters

The main thing is though I still have to be able to have each loop outputing the format like so

Distric    1 Digit     1 Digit & 2 Characters
Distric    2 Digit     1 Digit & 2 Characters
Distric    1 Digit & 1 Character     1 Digit & 2 Characters

Any help would be great

many thanks
Steve
For Each r1 As String In District
            s1 = r1
 
            For Each r2 As String In Digit1
                s2 = s1 & r2 & " "
                For Each r3 As String In Digit1
                    s3 = s2 & r3
                    For Each r4 As String In Alpha2
                             '......do stuff
                    Next
                Next
            Next
 
            'Pass 2
            For Each r2 As String In Digit2
                s2 = s1 & r2 & " "
                For Each r3 As String In Digit1
                    s3 = s2 & r3
                    For Each r4 As String In Alpha2
                             '......do stuff
                    Next
                Next
            Next
 
            'Pass 3
            For Each r2 As String In Digit1
                s2 = s1 & r2
                For Each r3 As String In Alpha1
                    s3 = s1 & s2 & r3 & " "
                    For Each r4 As String In Digit1
                        s4 = s3 & r4
                        For Each r5 As String In Alpha2
                             '......do stuff
                        Next
                    Next
                Next
            Next
        Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Stephen Manderson

ASKER

Thanks, was thinking along them lines.

Generated a list for the district, middle and end :-) got my 300 + mil combinations now.. opened another question about resuming the loop if stopped if you can help?