问题描述
假设我有下表
身份证 |名字 |姓氏 |总收入
3 |布拉布拉 |布拉巴 | 150000
任务是找到 ID 号为 3 的行,并在消息框中打印出“Blabla Blabla 总共产生了 150000 收入”
我是 VBA 新手。你能帮我吗?
解决方法
可以使用 &
运算符连接单元格值以及 VBA 中的其他字符串。
例如: 要以消息框中提供的格式打印第 3 行单元格值,请使用以下子例程:
Sub mbox()
MsgBox Cells(4,2) & " " & Cells(4,3) & " has generated " & Cells(4,4) & " revenue overall."
End Sub
,
使用 Replace
Option Explicit
Sub test()
Call message(3)
End Sub
Sub message(id As String)
Const msg = "? has generated $ revenue overall."
Dim rng As Range,s As String
Set rng = Range("Table1[ID]").Find(id)
If rng Is Nothing Then
MsgBox "Could not locate " & id,vbExclamation
Else
s = Replace(msg,"?",rng.Offset(0,1) & " " & rng.Offset(0,2))
s = Replace(s,"$",Format(rng.Offset(0,3),"#,##0"))
MsgBox s
End If
End Sub