quyenpv
Thu nhặt kiến thức
- Tham gia
- 5/1/13
- Bài viết
- 729
- Được thích
- 97
- Giới tính
- Nam
- Nghề nghiệp
- Decode cuộc đời!
Em chào anh chị!
Em có 1 đoạn code để tạo ra file kml dùng cho Google Map. Em muốn tạo thư mục cho lớp huyện sau đó đến lớp xã và cuối cùng là lớp cho các địa điểm.
Dữ liệu ban đầu

Thường tạo bằng tay thì nó sẽ như thế này

Code của em sưu tầm và chỉnh sửa đang bị sai vòng lặp dẫn đến khi chạy

Code
Em có 1 đoạn code để tạo ra file kml dùng cho Google Map. Em muốn tạo thư mục cho lớp huyện sau đó đến lớp xã và cuối cùng là lớp cho các địa điểm.
Dữ liệu ban đầu

Thường tạo bằng tay thì nó sẽ như thế này

Code của em sưu tầm và chỉnh sửa đang bị sai vòng lặp dẫn đến khi chạy

Code
Mã:
Sub Create_KML()
' Dim cLimit As Range
' Dim dLimit As Range
Dim SiteID(), SiteName(), Group_Tinh(), Group_Huyen(), Colour(), Data0(), Data1(), Data2(), Data3(), Data4(), Data5(), StrLat(), StrLon() As String
Dim Lat(), Lon() As Single
Dim errFlag As Boolean
'Find the last cell in the x y columns
cLimit = Range("C6").End(xlDown).Row
dLimit = Range("D6").End(xlDown).Row
rlimit = Range("A5").End(xlToRight).Column
'Test for data completeness
If cLimit <> dLimit Then
MsgBox "Error: Number of Lat & Long coordinates do not match. Please correct."
Exit Sub
End If
'Sort for the kml group
Range(Cells(6, 1), Cells(dLimit, rlimit)).Select
Selection.Sort key1:=Range("E6"), _
order1:=xlAscending, Header:=xlNo
aSize = cLimit - 5
ReDim StrLat(aSize), StrLon(aSize), SiteID(aSize), SiteName(aSize), Group_Tinh(aSize), Group_Huyen(aSize), Colour(aSize), Data0(aSize), Data1(aSize), Data2(aSize), Data3(aSize), Data4(aSize), Data5(aSize)
ReDim Lat(aSize), Lon(aSize)
Data0(0) = Cells(5, 7).Value
Data1(0) = Cells(5, 8).Value
Data2(0) = Cells(5, 9).Value
Data3(0) = Cells(5, 10).Value
Data4(0) = Cells(5, 11).Value
Data5(0) = Cells(5, 12).Value
Range("A5").Select
For rCount = 1 To cLimit - 5
rPosition = rCount + 5
SiteID(rCount) = Cells(rPosition, 1).Value
SiteName(rCount) = Cells(rPosition, 2).Value
Group_Tinh(rCount) = Cells(rPosition, 5).Value
Group_Huyen(rCount) = Cells(rPosition, 6).Value
Colour(rCount) = Cells(rPosition, 7).Value
Data0(rCount) = Cells(rPosition, 8).Value
Data1(rCount) = Cells(rPosition, 9).Value
Data2(rCount) = Cells(rPosition, 10).Value
Data3(rCount) = Cells(rPosition, 11).Value
Data4(rCount) = Cells(rPosition, 12).Value
Data5(rCount) = Cells(rPosition, 13).Value
Lat(rCount) = Cells(rPosition, 3).Value
Lon(rCount) = Cells(rPosition, 4).Value
Next rCount
'MsgBox Data0(0) & Data1(0) & Data2(0) & Data3(0)
'Fail the conversion if the Lat/Long values are out of range
errFlag = False
For rCount = 1 To cLimit - 5
If Lat(rCount) > 90 Then errFlag = True
If Lat(rCount) < -90 Then errFlag = True
If Lon(rCount) > 180 Then errFlag = True
If Lon(rCount) < -180 Then errFlag = True
If errFlag = True Then
MsgBox "Coordinates are out of range"
Exit Sub
End If
Next rCount
' Fix regional comma/decimal point
For rCount = 1 To cLimit - 5
If InStr(1, CStr(Lat(rCount)), ",") > 0 Then
tmp = CStr(Lat(rCount))
StrLat(rCount) = Replace(tmp, ",", ".")
Else
StrLat(rCount) = CStr(Lat(rCount))
End If
If InStr(1, CStr(Lon(rCount)), ",") > 0 Then
tmp = CStr(Lon(rCount))
StrLon(rCount) = Replace(tmp, ",", ".")
Else
StrLon(rCount) = CStr(Lon(rCount))
End If
Next rCount
'Get FileName
tName = "KML_Export_" & Format(Date, "yyyymmdd")
fName = Application.GetSaveAsFilename(FileFilter:= _
"KML Files (*.kml), *.kml", Title:="KML File Export", _
InitialFileName:=tName)
If fName = False Then Exit Sub
'Count the number of folders to create
gNum = 0
gName = ""
For rCount = 1 To cLimit - 5
If Group_Tinh(rCount) <> gName Then
gNum = fNum + 1
gName = Group_Tinh(rCount)
End If
Next rCount
hNum = 0
hName = ""
For i = 1 To cLimit - 5
If Group_Huyen(i) <> hName Then
hNum = fhNum + 1
hName = Group_Huyen(i)
End If
Next i
'MsgBox hName
'Open & populate output file
Open fName For Output As #1
Call writeKmlInitial(GetFilenameFromPath(fName), 1)
'
'Open kml Group_Tinh/folder
If gNum > 0 Then
If Group_Tinh(1) = "" Then
gName2 = "Undefined"
Else
gName2 = Group_Tinh(1)
End If
Print #1, " <Folder>"
Print #1, " <name>" & gName2 & "</name>"
Print #1, " <open>1</open>"
Print #1, " <gx:balloonVisibility>1</gx:balloonVisibility>"
End If
'Populate the points
gName = Group_Tinh(1)
For rCount = 1 To cLimit - 5
If gNum > 0 Then
If Group_Tinh(rCount) <> gName Then
gName = Group_Tinh(rCount)
If gName = "" Then
gName2 = "Undefined"
Else
gName2 = gName
End If
Print #1, " </Folder>"
Print #1, " <Folder>"
Print #1, " <name>" & gName2 & "</name>"
Print #1, " <open>1</open>"
Print #1, " <gx:balloonVisibility>1</gx:balloonVisibility>"
End If
End If
'Open kml Group_Tinh/folder
If hNum > 0 Then
If Group_Huyen(1) = "" Then
hName2 = "Undefined"
Else
hName2 = Group_Huyen(1)
End If
Print #1, " <Folder>"
Print #1, " <name>" & hName2 & "</name>"
Print #1, " <open>1</open>"
Print #1, " <gx:balloonVisibility>1</gx:balloonVisibility>"
End If
'Populate the points
hName = Group_Huyen(1)
For i = 1 To cLimit - 5
If hNum > 0 Then
If Group_Huyen(i) <> hName Then
hName = Group_Huyen(i)
If hName = "" Then
hName2 = "Undefined"
Else
hName2 = hName
End If
Print #1, " </Folder>"
Print #1, " <Folder>"
Print #1, " <name>" & hName2 & "</name>"
Print #1, " <open>1</open>"
Print #1, " <gx:balloonVisibility>1</gx:balloonVisibility>"
End If
End If
Print #1, " <Placemark>"
Print #1, " <name>" & SiteID(i) & "</name>"
Print #1, " <LookAt>"
Print #1, " <longitude>" & StrLon(i) & "</longitude>"
Print #1, " <latitude>" & StrLat(i) & "</latitude>"
Print #1, " <altitude>0</altitude>"
Print #1, " <heading>-1.539914092246387e-008</heading>"
Print #1, " <tilt>0</tilt>"
Print #1, " <range>640383.0131348133</range>"
Print #1, " <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>"
Print #1, " </LookAt>"
Print #1, " <Point>"
Print #1, " <gx:drawOrder>1</gx:drawOrder>"
Print #1, " <coordinates>" & StrLon(i) & "," & StrLat(i) & ",0</coordinates>"
Print #1, " </Point>"
Print #1, " </Placemark>"
Next i
'Finish off the folder/group
If hNum > 0 Then
Print #1, " </Folder>"
End If
Next rCount
'Finish off the folder/group
If gNum > 0 Then
Print #1, " </Folder>"
End If
'Finish off the kml document
Print #1, "</Document>"
Print #1, "</kml>"
Close #1
MsgBox "Conversion Complete, see: " & fName
End Sub