好的。以下是一段生成 Excel 报价器的 VBA 代码示例:
Sub CreateQuotation()
' 定义变量
Dim product As String
Dim price As Double
Dim quantity As Integer
Dim total As Double
' 获取用户输入的产品名称、单价和数量
product = InputBox("请输入产品名称:")
price = InputBox("请输入单价:")
quantity = InputBox("请输入数量:")
' 计算总价
total = price * quantity
' 在工作表中插入新行并填写产品名称、单价、数量和总价
With ActiveSheet
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).EntireRow.Insert
.Cells(.Rows.Count, 1).End(xlUp).Value = product
.Cells(.Rows.Count, 2).End(xlUp).Value = price
.Cells(.Rows.Count, 3).End(xlUp).Value = quantity
.Cells(.Rows.Count, 4).End(xlUp).Value = total
End With
End Sub
这段代码实现了以下功能:
- 获取用户输入的产品名称、单价和数量。
- 计算总价。
- 在工作表中插入新行并填写产品名称、单价、数量和总价。
如果你想要在生成报价单时自动计算总价,你还可以添加一个按钮,点击按钮时调用上面的代码,实现自动生成报价单的功能。
希望这些内容对你有帮助。