使用VBA实现快递单号查询功能通过msgbox窗口显示查询结果 |
作者:
来源:原创
|
如下图:左侧为部分原始数据,通过点击右侧的快递单查询(使用VBA功能),最后的结果展示是以弹出窗口的形式显示。
VBA代码
Option Explicit
Sub 快递单号() Dim num As String num = InputBox("请输入要查询的快递单号:") Dim arr() arr = Array("快递单号", "目的地", "重量", "快递费") Dim rMax As Integer rMax = ActiveSheet.UsedRange.Rows.Count Debug.Print rMax Dim i As Integer For i = 1 To rMax If Cells(i, 5) = num Then Dim arr1(1 To 4) arr1(1) = Cells(i, 5) arr1(2) = Cells(i, 6) arr1(3) = Cells(i, 7) arr1(4) = Cells(i, 8) MsgBox (arr(0) & ": " & arr1(1) & Chr(13) & arr(1)) & ": " & arr1(2) & Chr(13) & arr(2) & ": " & arr1(3) & Chr(13) & arr(3) & ": " & arr1(4) & Chr(13) End End If Next MsgBox ("未查询到此快递单号!") End Sub |
如果查询的快递单号不存在,则会提示如下信息:
|
|
|
|