VBA 实现调用地图查询网页,匹配地址查询所属省份

这个博客介绍了如何使用Excel VBA进行批量地址查询,通过调用网页接口获取省份信息,并根据查询结果实施物流配送策略。VBA代码实现了两个模块:模块1针对单个地址单元格查询,模块2循环遍历所有地址。在模块3中,根据地址省份及重量总和判断适用的物流公司,如当总重量达到一定阈值且地址为陕西省时选择‘跨越’,否则选择‘顺丰’。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模块1,选中单个地址单元格查询省份

'查询选中地址
Sub addressAcquisition()
Dim ie As Object
Dim url As String
col = Application.WorksheetFunction.CountA(Columns(1))
'For i = 2 To col


'获取地址网址
url = "https://scorpionfree98.github.io/fetch_address_detail/Geocoder.html"

Set ie = CreateObject("InternetExplorer.Application")
'网页可见性
ie.Visible = False
            
ie.navigate url
'等待网页加载完毕
While ie.readyState <> 4 Or ie.Busy = True
    DoEvents
Wend

'输入搜索框
ie.Document.getElementById("address").Value = Selection.Value
'点击搜索
ie.Document.getElementById("geo").Click

While ie.readyState <> 4 Or ie.Busy = True
    DoEvents
Wend

ro = Selection.Row

'获取结果值
r = ie.Document.getElementById("province").Value
Cells(ro, 2) = r
'关闭网页
ie.Quit


'Next i
End Sub



模块2,循环查找所有地址

'查询所有地址
Sub addressAcquisitions()

Dim ie As Object
Dim url As String
col = Application.WorksheetFunction.CountA(Columns(1))
For i = 2 To col


'获取地址网址
url = "https://scorpionfree98.github.io/fetch_address_detail/Geocoder.html"

Set ie = CreateObject("InternetExplorer.Application")
'网页可见性
ie.Visible = False
            
ie.navigate url
'等待网页加载完毕
While ie.readyState <> 4 Or ie.Busy = True
    DoEvents
Wend

'输入搜索框
ie.Document.getElementById("address").Value = Cells(i, 1)
'点击搜索
ie.Document.getElementById("geo").Click

While ie.readyState <> 4 Or ie.Busy = True
    DoEvents
Wend


'获取结果值
r = ie.Document.getElementById("province").Value
Cells(i, 2) = r
'关闭网页
ie.Quit

Next i
End Sub




模块3,部分判断

Sub totalWeight()

Range("F11") = 0

'判断选择的地址是否相同
Add1 = Cells(Selection.Row, 1)
For Each d In Selection
Add2 = Cells(d.Row, 1)
If Add1 <> Add2 Then
MsgBox ("地址不同")
Exit For
Else
Add1 = Add2
End If
Next


For Each d In Selection

Range("F11") = Range("F11") + d.Value
province = Cells(d.Row, 2)
Next

If province = "陕西省" And Range("F11") >= 80 Then
   Range("F12") = "跨越"
    ElseIf province = "陕西省" And Range("F11") < 80 Then
    Range("F12") = "顺丰"
    ElseIf province <> "陕西省" And Range("F11") >= 30 Then
   Range("F12") = "跨越"
    ElseIf province <> "陕西省" And Range("F11") < 30 Then
    Range("F12") = "顺丰"
    End If


End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值