这可能在这里某处,我错过了,只是让我知道。VBA运行时错误7内存不足
运行我的宏后,我得到运行时错误7内存不足。经过调试,这是在这条线:
cell.Offset(0, 6).Value = Sheets("MakeRing_Maths").Range("N1").Value
的代码是为了跑下来与经度和纬度机场的列表,创建将被解释为圆线段,并转换为.KML(这是读由Google地球专业版)。
下面是其余的代码 - 我如何清理这样的事情,以避免内存泄漏?
所有答案赞赏,或指向其他职位。我知道这是很多,所以一般的建议也欢迎!谢谢!
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 & Name")
' file details
filePath = ThisAddress
docName = "PLANEMAN.KML"
FolderName = "Folder"
Open filePath For Output As #1
'Write header to file
outputText = "<?xml version=""1.0"" encoding=""UTF-8""?> <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""> <Document><name>" & docName & "</name> <Folder> <name>" & FolderName & "</name> <open>1</open>"
Print #1, outputText
'loop
For Each cell In [RangeRings_ENTER!B9:B5001]
If cell.Value = "" Then
Exit For
End If
StrPart1 = "<Style id=""sn_ylw-pushpin""><IconStyle><color>" & cell.Offset(0, 3) & "</color></IconStyle><LineStyle><width>" & cell.Offset(0, 2) & "</width><color>" & cell.Offset(0, 3) & "</color></LineStyle><PolyStyle><color>" & cell.Offset(0, 3) & "</color></PolyStyle></Style>"
StrPart2 = "<Placemark><name>" & cell.Offset(0, -1) & "</name> <styleUrl>#sn_ylw-pushpin</styleUrl> <LineString> "
StrPart3 = "<coordinates>" & cell.Offset(0, 6) & ",0 </coordinates> </LineString></Placemark>"
'Create a placemark
outputText = StrPart1 & StrPart2 & StrPart3
Print #1, outputText
Next
'Write footer to file
outputText = "</Folder></Document></kml>"
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 & Name")
' file details
filePath = ThisAddress
docName = "PLANEMAN.KML"
FolderName = "PlacemarkFolder"
Open filePath For Output As #1
'Write header to file
outputText = "<?xml version=""1.0"" encoding=""UTF-8""?><kml xmlns=""http://www.opengis.net/kml/2.2""> <Document><name>" & docName & "</name> <Folder> <name>" & FolderName & "</name> <open>1</open>"
Print #1, outputText
'loop
For Each cell In [Placemarks_ENTER!B9:B5001]
If cell.Value = "" Then
Exit For
End If
StrPart1 = ""
StrPart2 = " <Placemark> <name> " & cell.Offset(0, -1) & " </name> "
StrPart3 = cell.Offset(0, 6) & "<Point><coordinates> " & cell.Offset(0, 0) & "," & cell.Offset(0, 1) & ",0</coordinates> </Point> </Placemark>"
'Create a placemark
outputText = StrPart1 & StrPart2 & StrPart3
Print #1, outputText
Next
'Write footer to file
outputText = "</Folder></Document></kml>"
Print #1, outputText
Close #1
MsgBox "Macro Complete"
'
End Sub
您最初的'If'声明没有'Else'条款,但你有'Else'声明那里。如果'Else'没有代码要执行时,请去除'Else' - 否则它看起来像你忘记了一些东西。 – FreeMan