我有一個電子表格,包含大量數據(氣象站的目錄),它可以計算用戶輸入緯度和經度的最近氣象站。這個工作表通過計算距輸入點的距離,使用SMALL()對這些距離進行排序,然後使用排名(1是最接近的,2是第二接近的等),然後是具有公式的Excel表/列表執行Index(Match 。Excel VBA宏 - 通過過濾表的列循環
工作表雖然很慢,工作得很好 - 而且Excel表允許按照各種標準(如年限等記錄的長度)對氣象站目錄進行高級排序。
我有一個VBA宏,我正在寫它曾經工作,但停止工作,當我試圖修復它(真棒)。
VBA宏的目的是編寫經緯度/氣象站名稱的Google地球KML文件,然後將該文件啓動到谷歌地球,以便用戶可以在設置的站點位置周圍可視化鄰近站點(之前由用戶輸入的那個)。
不幸的是,我使用的原始方法無法處理列表的過濾結果,例如,如果用戶過濾了結果(例如,前4個氣象站被過濾掉的例子),宏仍然會寫前四個不可見/被過濾的氣象站。
對我來說問題變得更加困難,因爲我希望只有一個具有可篩選表格的工作表的宏 - 針對不同的數據類型。
在這個階段,宏需要的數據存儲在不同工作表中的名稱相同的表列中:{「STATION」,「LONGITUDE」,「LATITUDE」}。寫入KML文件所需的大部分KML字符串都存儲在另一個隱藏工作表「KML」中。
宏通過這些頁面上的按鈕啓動。
我明白,可以使用「.SpecialCells(xlCellTypeVisible)」的解決方案 - 我已經嘗試廣泛使它與我的表一起工作 - 但迄今爲止還沒有運氣 - 可能是由於我缺乏正規訓練。
任何幫助表示讚賞,無論是解決方案或建議!我糟糕的代碼道歉,問題環路&斷碼的面積縮小到一半 - 後「查找主動表中的所有表:
Sub KML_writer()
Dim FileName As String
Dim StrA As String
Dim NumberOfKMLs
Dim MsgBoxResponse
Dim MsgBoxTitle
Dim MsgBoxPrompt
Dim WhileCounter
Dim oSh As Worksheet
Set oSh = ActiveSheet
'Prompt the Number of Stations to Write to the KML File
NumberOfKMLs = InputBox(Prompt:="Please Enter the number of Weather Stations to generate within the Google Earth KML file", _
Title:="Number of Weather Stations", Default:="10")
'Prompt a File Name
FileName = InputBox(Prompt:="Please Enter a name for your KML File.", _
Title:="Lat Long to KML Converter", Default:="ENTER FILE NAME")
'Will clean this up to not require Write to Cell and Write to KML duplication later
Sheets("kml").Range("B3").Value = FileName
Sheets("mrg").Range("C5").Value = "Exported from EXCEL by AJK's MRG Function"
saveDir = "H:\" 'Local Drive available for all users of macro
targetfile = saveDir & FileName & ".KML"
'Write Site Location to KML STRING - user entered values from SITE LOCATION worksheet
StrA = Sheets("kml").Range("B1").Value & Sheets("kml").Range("B2").Value & "SITE LOCATION" & Sheets("kml").Range("B4").Value & Sheets("INPUT COORDINATES").Range("E5").Value & Sheets("kml").Range("B6").Value & Sheets("INPUT COORDINATES").Range("E4").Value & Sheets("kml").Range("B8").Value
'Find all tables on active sheet
Dim oLo As ListObject
For Each oLo In oSh.ListObjects
'
Dim lo As Excel.ListObject
Dim lr As Excel.ListRow
Set lo = oSh.ListObjects(oLo.Name)
Dim cl As Range, rng As Range
Set rng = Range(lo.ListRows(1)) 'this is where it breaks currently
For Each cl In rng2 '.SpecialCells(xlCellTypeVisible)
'Stop looping when NumberofKMLs is written to KML
WhileCounter = 0
Do Until WhileCounter > (NumberOfKMLs - 1)
WhileCounter = WhileCounter + 1
Dim St
Dim La
Dim Lon
'Store the lr.Range'th station data to write to the KML
St = Intersect(lr.Range, lo.ListColumns("STATION").Range).Value
La = Intersect(lr.Range, lo.ListColumns("LATITUDE").Range).Value
Lon = Intersect(lr.Range, lo.ListColumns("LONGITUDE").Range).Value
'Write St La Long & KML Strings for Chosen Stations
StrA = StrA & Sheets("kml").Range("B2").Value & St & Sheets("kml").Range("B4").Value & Lon & Sheets("kml").Range("B6").Value & La & Sheets("kml").Range("B8").Value
Loop
Next
Next
'Write end of KML strings to KML File
StrA = StrA & Sheets("kml").Range("B9").Value
'Open, write, close KML file
Open targetfile For Output As #1
Print #1, StrA
Close #1
'Message Box for prompting the launch of the KML file
MsgBoxTitle = ("Launch KML?")
MsgBoxPrompt = "Would you like to launch the KML File saved at " & targetfile & "?" & vbCrLf & vbCrLf & "Selecting 'No' will not prevent the file from being written."
MsgBoxResponse = MsgBox(MsgBoxPrompt, vbYesNo, MsgBoxTitle)
If MsgBoxResponse = 6 Then ThisWorkbook.FollowHyperlink targetfile
End Sub
啊啊啊我看你已經提到過這種方法。讓我舉個小例子來讓你開始,今晚晚些時候我有機會。 –