|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
代码的功能就是查快递的发出地和流转现状,群里相关的帖子都看过了,借鉴了非常多,拼凑出了我想要的代码,但是运行一两百个就不行了,希望能优化。- Function kkk()
- Dim sHTML As String '查询返回的HTML值
- Dim sRes As String
- Dim iTimeOut As Integer
- Dim iTimeOutMax As Integer
- Dim aFile As Variant
- Dim arrLine As Variant
- Dim sLine As String '每行跟踪记录
- Dim arrText As Variant
- Dim sText As String '在每行跟踪记录中用于分段获取时间和日志
- Dim iCnt As Integer '循环变量
- Dim iRow As Integer '写入行号
- Dim i As Integer
- Dim sTime As String '每条跟踪记录中对应的时间
- Dim sLog As String '每条跟踪记录中对应的日志
- Dim sNum As String
- Dim sTypeSelect As String
- Dim sType As String
- Dim sURL As String
- Dim iTimeCol As Integer
- Dim iLogCol As Integer
- On Error Resume Next
- Range("f2:i1000").ClearContents '先清除旧结果
- i = 2
- Do While Cells(i, 1) <> ""
- iRow = 2 '从第2行开始写结果
- iTimeCol = 4 '写时间的列号
- iLogCol = 5 '写跟踪记录的列号
- iTimeOutMax = 8000 '查询超时设置
-
- sNum = Cells(i, 2).Value '快递单号
- sTypeSelect = Cells(i, 1).Value '快递公司选择
- '下面是将快递公司的中文名转换成对应的代码(应该都是汉语全拼)可以加上其它的快递公司
- If sTypeSelect = "中通" Then
- sType = "zhongtong"
- ElseIf sTypeSelect = "申通" Then
- sType = "shentong"
- ElseIf sTypeSelect = "圆通" Then
- sType = "yuantong"
- ElseIf sTypeSelect = "顺丰" Then
- sType = "shunfeng"
- ElseIf sTypeSelect = "EMS" Then
- sType = "ems"
- ElseIf sTypeSelect = "百世" Then
- sType = "huitongkuaidi"
- ElseIf sTypeSelect = "宅急送" Then
- sType = "zhaijisong"
- ElseIf sTypeSelect = "韵达" Then
- sType = "yunda"
- Else
- Cells(i, 4).Value = "'该快递公司无法识别:" & sTypeSelect
- Exit Function
- End If
-
- '拼接URL地址
- sURL = "http://www.kuaidi100.com/query?type=" & sType & "&postid=" & sNum '借用快递100的查询接口
- Dim ET As New InternetExplorer
- ET.Visible = False '隐藏IE
- ET.Navigate sURL '输入网址
- iTimeOut = 1
- Do Until ET.ReadyState = 4 Or iTimeOut > iTimeOutMax '等待页面加载完毕
- iTimeOut = iTimeOut + 1
- Loop
- sHTML = ET.Document.body.innerHTML
- Set ET = Nothing
- '以下开始解析返回的查询结果
- If iTimeOut >= iTimeOutMax Then
- Cells(iRow, iLogCol).Value = "查询超时"
- ElseIf sHTML = "" Then
- Cells(iRow, iLogCol).Value = "查询失败!"
- Else
- aFile = Split(sHTML, """data"":[{")
- If UBound(aFile) >= 1 Then
- sLine = aFile(1)
- arrLine = Split(sLine, "},{")
- For iCnt = 0 To UBound(arrLine)
- sText = arrLine(iCnt)
- arrText = Split(sText, """:""")
- If UBound(arrText) >= 2 Then
- sTime = arrText(1)
- sLog = arrText(3)
- sTime = Rep(sTime)
- sLog = Rep(sLog)
- Cells(iRow, iTimeCol).Value = "'" & sTime
- Cells(iRow, iLogCol).Value = "'" & sLog
- iRow = iRow + 1
- End If
- Next
- sRes = aFile(0)
- Else
- aFile = Split(sHTML, ":")
- sRes = aFile(2)
- Cells(iRow, iLogCol).Value = Rep(sRes)
- End If
- Cells(i, 6) = Cells(iRow - 1, iTimeCol)
- Cells(i, 7) = Cells(iRow - 1, iLogCol)
- Cells(i, 8) = Cells(2, iTimeCol)
- Cells(i, 9) = Cells(2, iLogCol)
- End If
- i = i + 1
- Loop
- End Function
- '替换掉无用的字符串
- Function Rep(str As String) As String
- Dim sRes As String
- sRes = Replace(str, """", "")
- sRes = Replace(sRes, "{", "")
- sRes = Replace(sRes, "}", "")
- sRes = Replace(sRes, ",context", "")
- sRes = Replace(sRes, ",ftime", "")
- sRes = Replace(sRes, ",location", "")
- Rep = sRes
- End Function
复制代码
|
|