Gaining efficiency in VBA Loop, Minimise Traffic

TrickyB89

New Member
Joined
Apr 3, 2018
Messages
2
Hi,

Hoping for a way to speed up this loop. Currently this VBA transfers two values at a time (Latitude and Longitude) as inputs into a calculator on a separate worksheet which calculates the distance from the start of the road based on proximity to other points (number of different functions/look ups). The outputs generated from the "calculator sheet" (i.e road chainage, road name, offset from road centre line etc.) are then transferred back alongside the original coordinates.

I was thinking reading the Latitude and Longitude into VBA somehow to minimise traffic between VBA and worksheet.

Please see below extract:

Code:
Sub Chainage()


Dim Chain As Range
Dim Name As Range
Dim Offset As Range




  Application.ScreenUpdating = False
    'Start to loop
    For Each Cell In [A2.A5000]
        Lat = Cell.Offset(0, 0)
        Lon = Cell.Offset(0, 1)
                
        If Not IsEmpty(Lon) Then
        Range("ChainageCalculator!B7").Value = Lat
        Range("ChainageCalculator!C7").Value = Lon
                
        Set Chain = Sheets("ChainageCalculator").Cells(7, 4)
        Set Name = Sheets("ChainageCalculator").Cells(7, 7)
        Set Offset = Sheets("ChainageCalculator").Cells(7, 5)


        Cell.Offset(0, 3) = Chain
        Cell.Offset(0, 2) = Name
        Cell.Offset(0, 4) = Offset
        
        End If
  
    Next
        
   Application.ScreenUpdating = True
  
    End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Welcome to the Forum!

I was thinking reading the Latitude and Longitude into VBA somehow to minimise traffic between VBA and worksheet.

Yes, spot on. Try:

Code:
Sub Chainage()

    Dim LastRow As Long, i As Long
    Dim vResults As Variant
    
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    vResults = Range("A2:E" & LastRow).Value
    
    Application.ScreenUpdating = False
    
    With Worksheets("ChainageCalculator")
        For i = 1 To UBound(vResults)
            .Range("B7").Value = vResults(i, 1)   'Lat
            .Range("C7").Value = vResults(i, 2)   'Lon
            vResults(i, 3) = .Range("G7").Value  'Name
            vResults(i, 4) = .Range("D7").Value 'Chain
            vResults(i, 5) = .Range("E7").Value 'Offset
        Next i
    End With
    
    Range("A2:E2").Resize(UBound(vResults)).Value = vResults
        
    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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