VBA For while loop help

Weeble

Board Regular
Joined
Nov 30, 2016
Messages
95
Office Version
  1. 365
Hi! I'm need of abit of help here. Prob taken water over my head.
I'm a really green VBA coder. Been trying to google and youtube for solutions to my problem. Right now I am doing this manualy with abit of coding. But was wondering if there might be a fully automated solution to this.



My 'Blad1'(sheet1) contains a table with this information.
ABCDE
1CityOrderBarcodeArticlezone
2Atlantis123151612CrystalOCE
3Atlantis123151613BoatOCE
4Atlantis123151511OarsLAN
5Atlantis123151234Clay statueCRA
6Atlantis123151236SomethingLAN
7Atlantis123151612FacepaintOCE
8Atlantis123151612PlantOCE
9Atlantis123151619OceanOCE
10Pluto321163215Rock 1LAN
11Pluto321132168Rock 2LAN
12Pluto321133521Rock 3LA1
13Atlantis321132168Rock 4OCE
14Pluto321132168Rock 5LAN
15Pluto321132168Rock 6LAN
16Pluto321132168Rock 7LAN

<tbody>
</tbody>
Blad1



My 'Blad2'(sheet2) has this information

EFGH
2City :Atlantis
3ZoneOCE
4
5Unique Barcodes:151612
6151613
7151619

<tbody>
</tbody>
Blad2



My goal.

I want to have a VBA For While loop that does the following.

  1. From 'Blad1' Copy first unique value in column A into 'Blad2' F2(city).
  2. Then I want to copy the first unique value from 'Blad1' column E(zone), that matches F2(city) in 'Blad2'F2. into 'Blad2' F3
  3. I then want to add a line that prints 'Blad2'
  4. Then I need to check if column E in 'Blad1' has another unique value that matches F2(city) from 'Blad2', If so, copy that next value into 'Blad2' F3
  5. IF there was a new unique value, print 'Blad2' again.
For looping thrue this, it should now have found.

  • City : Atlantis
  • Zone: OCE
  • Printed
And also copied LAN to Zone

  • Printed
And also copied CRA to Zone


  • Printed

Next step the FOR loop should not have found any more unique values of ZONES with the city Atlantis.
Then it should continue and look for the next unique CITY name in 'Blad1' which is Pluto.
Then continue the previous loop, but with the new unique value of Pluto.

Maybe this became more complicated then I originaly expected.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The way your scenario reads it would print a full page with a single item for the first loop,
Then, since sheet2 already has that item listed, when you add the second zone data, it would print a full page with only two items of data.
Then, a third item would be added to sheet2 and a full page printed with three items of data.
It seems to me that if you used sheet two to create a list of items similar to the one below.
City
Zone
Atlantis
OCE
Atlantis
LAN
Atlantis
CRA
Pluto
LAN
Pluto
LA1

<TBODY>
</TBODY>

then you could just print the sheet 1 time and save some paper. But maybe you need the separate sheets. If so, do you want to accumulate the data for each loop, or would you want to delete data from the previous loop and only print data from the current loop?

Also, I note that you have unique barcodes listed in your Blad2 example, but there is no mention of them in the description of what you want to copy from Blad1. Did you inadvertantly omit those from the copy requirement?
 
Last edited:
Upvote 0
Sorry for the late late reply JLG, I ran into a whole bunch of problem with other stuff that held me back for a while.
I'll try and explain what 'Blad2' is used for. I : M generates a list from 'Blad1' depending on that F2 and F3 has.
The Barcodes is also data from 'Blad1' Table. So this loop is to go thrue all The Cities with their respective zones, and print a sheet of paper for each zone. In 'Blad2'.

EFGHIJKLM
2City : Atlantis
3ZoneOCE
4
5Unique Barcodes:151612
6151613
7151619
8132168List:Atlantis123151612CrystalOCE
9Atlantis123151613BoatOCE
10Atlantis123151612FacepaintOCE
11Atlantis123151612PlantOCE
12Atlantis123151619OceanOCE
13Atlantis321132168Rock 4OCE

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Blad2
 
Upvote 0
From your explaination in Post #3 , I assume that Blad 2 has formulas beginning in row 5 of Column F and in Columns I:M which will use the values in F2 and F3 to populate the applicable cells with data.
Code:
Sub t()
Dim i As Long, r As Range, fn As Range, c As Range
With Sheets("Sheet1")
    .Range("A1", .Cells(Rows.Count, 1).End(xlUp)).AdvancedFilter xlFilterCopy, , .Cells(Rows.Count, 1).End(xlUp)(3), True
    .Range("E1", .Cells(Rows.Count, 5).End(xlUp)).AdvancedFilter xlFilterCopy, , .Cells(Rows.Count, 5).End(xlUp)(3), True
    For Each c In .Cells(Rows.Count, 1).End(xlUp).CurrentRegion.Offset(1)
        If c <> "" Then
            For Each r In .Cells(Rows.Count, 5).End(xlUp).CurrentRegion.Offset(1)
                If r <> "" Then
                    Set fn = .Range("E:E").Find(r.Value, , xlValues)
                        If Not fn Is Nothing Then
                            If fn.Offset(, -4) = c.Value Then
                                With Sheets("Sheet2")
                                    .Range("F2:F3").ClearContents
                                    .Range("F2") = c.Value
                                    .Range("F3") = r.Value
                                    Sheets("Sheet2").PrintOut
                                End With
                            End If
                        End If
                End If
            Next
        End If
    Next
    .Cells(Rows.Count, 1).End(xlUp).CurrentRegion.ClearContents
    .Cells(Rows.Count, 5).End(xlUp).CurrentRegion.ClearContents
End With
End Sub
 
Last edited:
Upvote 0
Use this instead. I inadvertantly left out a loop to ensure all combinations were checked.

Code:
Sub t2()
Dim i As Long, r As Range, fn As Range, c As Range, adr As String
With Sheets("Sheet1")
    .Range("A1", .Cells(Rows.Count, 1).End(xlUp)).AdvancedFilter xlFilterCopy, , .Cells(Rows.Count, 1).End(xlUp)(3), True
    .Range("E1", .Cells(Rows.Count, 5).End(xlUp)).AdvancedFilter xlFilterCopy, , .Cells(Rows.Count, 5).End(xlUp)(3), True
    For Each c In .Cells(Rows.Count, 1).End(xlUp).CurrentRegion.Offset(1)
        If c <> "" Then
            For Each r In .Cells(Rows.Count, 5).End(xlUp).CurrentRegion.Offset(1)
                If r <> "" Then
                    Set fn = .Range("E:E").Find(r.Value, , xlValues)
                        If Not fn Is Nothing Then
                            adr = fn.Address
                            Do
                                If fn.Offset(, -4) = c.Value Then
                                    With Sheets("Sheet2")
                                        .Range("F2:F3").ClearContents
                                        .Range("F2") = c.Value
                                        .Range("F3") = r.Value
                                        Sheets("Sheet2").PrintOut
                                    End With
                                    Exit Do
                                End If
                                Set fn = .Range("E:E").FindNext(fn)
                            Loop While adr <> fn.Address
                        End If
                End If
            Next
        End If
    Next
    .Cells(Rows.Count, 1).End(xlUp).CurrentRegion.ClearContents
    .Cells(Rows.Count, 5).End(xlUp).CurrentRegion.ClearContents
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,179
Members
448,948
Latest member
spamiki

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