Loop through VBA

lalro143

New Member
Joined
May 30, 2014
Messages
39
Hi,

I have two sheets. One having warehouse(18 warehouse) and second having shop (50 Shops).

I need to map shops against warehouse within 200 KM.

I have written one code for the same but need to loop it. Code is mentioned below

Code will copy first warehouse from warehouse sheet and paste it against each all 50 shops in shop sheet.
Then one formula in the sheet (already updated in shop sheet) calculate the difference in KM between each shop and first warehouse. Then filtered out shops within 200 km, copy and paste (transpose) against first warehouse in warehouse sheet. Now I want to do same procedure for second warehouse then third till end. Please help



==================================
Sub RADIUS()


Worksheets("SHOP").Activate
AutoFilterMode = False
Range("f2:g4175").Clear
Worksheets("WAREHOUSE").Activate
Range("C2:d2").Select
Selection.Copy


Worksheets("SHOP").Activate
Range("f2:g4175").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
Application.Calculation = xlCalculationAutomatic


AutoFilterMode = True
Range("A1:h1").AutoFilter Field:=8, Criteria1:="<=200"
Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _


Worksheets("WAREHOUSE").Activate


Range("e" & Rows.Count).End(xlUp).Offset(1).Select


Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True


End Sub


=================================================
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Your quote
Now I want to do same procedure for second warehouse then third till end. Please help

Does each warehouse have a different sheet row or column.
Show us how the script would look when doing the second Warehouse versus the first warehouse.
 
Upvote 0
No all warehouse in only one sheet. Starts from cell c2 (lat) and cell d2 (lng)
below code copied first one which is in range(c2:d2) and pasted it to shop sheet to range (f2:g4175)
I dont know how to do it for next warehouse which is in c3:d3 cell till end of warehouses
===============
Worksheets("SHOP").Activate
AutoFilterMode = False
Range("f2:g4175").Clear
Worksheets("WAREHOUSE").Activate
Range("C2:d2").Select
Selection.Copy
=======================



 
Upvote 0
I have tried to do while loop but it showing some error


Sub RADIUS()
'Application.Workbooks.Open("D:\Current Reports & Updation\Lat Lng\All Petrol Pump\15KM DISTANCE.xlsx").Activate
'Application.Calculation = xlCalculateManual


Dim j As Integer
j = 2
Do While Worksheets("PP").Range("C" & j) <> ""


'Worksheets("PP").Activate
Range("C2:d2").Select
Selection.Copy


Worksheets("SITE").Activate
Range("f2:g4175").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
Application.Calculation = xlCalculationAutomatic


AutoFilterMode = True
Range("A1:h1").AutoFilter Field:=8, Criteria1:="<=15"
Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _


Worksheets("pp").Activate


Range("e" & Rows.Count).End(xlUp).Offset(1).Select


Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Worksheets("SITE").Activate
AutoFilterMode = False
Range("f2:g4175").Clear


j = j + 1
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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