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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,216,488
Messages
6,130,952
Members
449,608
Latest member
jacobmudombe

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