title: VBA Run-time Error 7 Out of Memory
body: <p>this might be on here somewhere and I missed it, just let me know.</p>
  
  <p>After running my macro, I get Run-time Error 7 Out of Memory. After debugging, it's on this line:</p>
  
  <pre><code>cell.Offset(0, 6).Value = Sheets("MakeRing_Maths").Range("N1").Value
  </code></pre>
  
  <p>The code is meant to run down a list of airports with longitudes and latitudes, create line segments that will be interpreted as circles, and convert to .KML (which is read by Google Earth Pro).</p>
  
  <p>Here is the rest of the code - how can I clean something like this up to avoid memory leaks??</p>
  
  <p>All answers appreciated, or pointers to other posts. I know this is a lot, so general suggestions are also welcome! Thank you!</p>
  
  <pre><code>Sub PLANEMAN_Coords()
  
  Dim Latitude As Double
  Dim Longitude As Double
  Dim Bearing As Integer
  Dim LeftRight As Integer
  Dim RangeKM As Double
  Dim MinRange As Double
  
  For Each cell In [RangeRings_ENTER!B9:B5001]
  
      If cell.Value = "" Then
          GoTo EXITLOOP
      Else
      End If
  
      Latitude = cell.Offset(0, 1)
      Longitude = cell.Value
  
      'set default values:
  
      'line width
      If cell.Offset(0, 2).Text = "" Then
          cell.Offset(0, 2).Value = 2
          'default line width = 2
      Else
      End If
  
      'radius
      If cell.Offset(0, 5).Text = "" Then
         cell.Offset(0, 5).Value = 8.04672
          'default radius = 8.04672 km = 5 miles
      Else
      End If
      RangeKM = cell.Offset(0, 5)
  
      'line color
      If cell.Offset(0, 3).Text = "" Then
          cell.Offset(0, 3).Value = "ff0000ff"
          'default line color is Red
      Else
      End If
  
      'common code
      Sheets("MakeRing_Maths").Range("D3").Value = Longitude
      Sheets("MakeRing_Maths").Range("E3").Value = Latitude
      Sheets("MakeRing_Maths").Range("D1").Value = RangeKM
  
      'code that differs depending on range-ring type
      If cell.Offset(0, 7).Text = "Circle" Then
          Sheets("MakeRing_Maths").Range("J1").Value = 0 'Bearing
          Sheets("MakeRing_Maths").Range("J2").Value = 180 'width - ie 2 x 180 = 360 = complete circle
          Calculate
          cell.Offset(0, 6).Select 'just so that the user can 'see' that the macro is still running and not crashed
          cell.Offset(0, 6).Value = Sheets("MakeRing_Maths").Range("N1").Value
      Else
          'else wedge of some sort
  
          Bearing = cell.Offset(0, 8)
          LeftRight = cell.Offset(0, 9)
          MinRange = cell.Offset(0, 10)
  
          Sheets("MakeRing_Maths").Range("J1").Value = Bearing
          Sheets("MakeRing_Maths").Range("J2").Value = LeftRight
  
          If cell.Offset(0, 7).Text = "Wedge" Then
              Calculate
              cell.Offset(0, 6).Select 'just so that the user can 'see' that the macro is still running and not crashed
              cell.Offset(0, 6).Value = Sheets("MakeRing_Maths").Range("N2").Value
          Else 'else a wedge with minimum range component 'Wedge2
              If cell.Offset(0, 7).Text = "Wedge2" Then
                  Sheets("MakeRing_Maths").Range("F1").Value = MinRange
                  Calculate
                  cell.Offset(0, 6).Select 'just so that the user can 'see' that the macro is still running and not crashed
                  cell.Offset(0, 6).Value = Sheets("MakeRing_Maths").Range("N3").Value
              Else
                  If cell.Offset(0, 7).Text = "Arrow" Then
                      Sheets("MakeRing_Maths").Range("F1").Value = RangeKM * 0.95
                      Calculate
                      cell.Offset(0, 6).Select 'just so that the user can 'see' that the macro is still running and not crashed
                      cell.Offset(0, 6).Value = Sheets("MakeRing_Maths").Range("N4").Value
                  Else
                      'HERE
                  End If
              End If
          End If
      End If
  Next
  
  EXITLOOP:
  
  Call PLANEMAN_RangeRings_KML 'make KML file
  
  End Sub
  
  
  
  Sub PLANEMAN_RangeRings_KML()
  ' Original inspiration code by simon_a
  ' Planeman 2009
  
      'get user to specify save location and name
      Dim ThisAddress As String
      ChDir ThisWorkbook.Path
      ThisAddress = Application.GetSaveAsFilename(FileFilter:="KML Files (*.kml),*.kml", Title:="Save Location &amp; Name")
  
      ' file details
      filePath = ThisAddress
      docName = "PLANEMAN.KML"
      FolderName = "Folder"
  
      Open filePath For Output As #1
  
      'Write header to file
      outputText = "&lt;?xml version=""1.0"" encoding=""UTF-8""?&gt; &lt;kml xmlns=""http://www.opengis.net/kml/2.2"" xmlns:gx=""http://www.google.com/kml/ext/2.2"" xmlns:kml=""http://www.opengis.net/kml/2.2"" xmlns:atom=""http://www.w3.org/2005/Atom""&gt; &lt;Document&gt;&lt;name&gt;" &amp; docName &amp; "&lt;/name&gt;    &lt;Folder&gt;    &lt;name&gt;" &amp; FolderName &amp; "&lt;/name&gt;  &lt;open&gt;1&lt;/open&gt;"
      Print #1, outputText
  
      'loop
      For Each cell In [RangeRings_ENTER!B9:B5001]
  
          If cell.Value = "" Then
             Exit For
          End If
  
          StrPart1 = "&lt;Style id=""sn_ylw-pushpin""&gt;&lt;IconStyle&gt;&lt;color&gt;" &amp; cell.Offset(0, 3) &amp; "&lt;/color&gt;&lt;/IconStyle&gt;&lt;LineStyle&gt;&lt;width&gt;" &amp; cell.Offset(0, 2) &amp; "&lt;/width&gt;&lt;color&gt;" &amp; cell.Offset(0, 3) &amp; "&lt;/color&gt;&lt;/LineStyle&gt;&lt;PolyStyle&gt;&lt;color&gt;" &amp; cell.Offset(0, 3) &amp; "&lt;/color&gt;&lt;/PolyStyle&gt;&lt;/Style&gt;"
          StrPart2 = "&lt;Placemark&gt;&lt;name&gt;" &amp; cell.Offset(0, -1) &amp; "&lt;/name&gt;  &lt;styleUrl&gt;#sn_ylw-pushpin&lt;/styleUrl&gt;    &lt;LineString&gt;    "
          StrPart3 = "&lt;coordinates&gt;" &amp; cell.Offset(0, 6) &amp; ",0 &lt;/coordinates&gt; &lt;/LineString&gt;&lt;/Placemark&gt;"
  
          'Create a placemark
          outputText = StrPart1 &amp; StrPart2 &amp; StrPart3
          Print #1, outputText
  
      Next
  
     'Write footer to file
      outputText = "&lt;/Folder&gt;&lt;/Document&gt;&lt;/kml&gt;"
      Print #1, outputText
  
      Close #1
  
      MsgBox "Macro Complete"
  
  '
  End Sub
  
  Sub PLANEMAN_Placemarks_KML()
  ' Original inspiration code by simon_a
  ' Planeman 2009
  
      'get user to specify save location and name
      Dim ThisAddress As String
      ChDir ThisWorkbook.Path
      ThisAddress = Application.GetSaveAsFilename(FileFilter:="KML Files (*.kml),*.kml", Title:="Save Location &amp; Name")
  
      ' file details
      filePath = ThisAddress
      docName = "PLANEMAN.KML"
      FolderName = "PlacemarkFolder"
  
      Open filePath For Output As #1
  
      'Write header to file
      outputText = "&lt;?xml version=""1.0"" encoding=""UTF-8""?&gt;&lt;kml xmlns=""http://www.opengis.net/kml/2.2""&gt; &lt;Document&gt;&lt;name&gt;" &amp; docName &amp; "&lt;/name&gt;    &lt;Folder&gt;    &lt;name&gt;" &amp; FolderName &amp; "&lt;/name&gt;  &lt;open&gt;1&lt;/open&gt;"
      Print #1, outputText
  
      'loop
      For Each cell In [Placemarks_ENTER!B9:B5001]
  
          If cell.Value = "" Then
             Exit For
          End If
  
          StrPart1 = ""
          StrPart2 = " &lt;Placemark&gt; &lt;name&gt; " &amp; cell.Offset(0, -1) &amp; " &lt;/name&gt; "
          StrPart3 = cell.Offset(0, 6) &amp; "&lt;Point&gt;&lt;coordinates&gt; " &amp; cell.Offset(0, 0) &amp; "," &amp; cell.Offset(0, 1) &amp; ",0&lt;/coordinates&gt; &lt;/Point&gt; &lt;/Placemark&gt;"
  
          'Create a placemark
          outputText = StrPart1 &amp; StrPart2 &amp; StrPart3
          Print #1, outputText
  
      Next
  
     'Write footer to file
      outputText = "&lt;/Folder&gt;&lt;/Document&gt;&lt;/kml&gt;"
      Print #1, outputText
  
      Close #1
  
      MsgBox "Macro Complete"
  
  '
  End Sub
  </code></pre>