Vba Word文档中的发票
我正在尝试编写一个VBA脚本,以便将ms word中表格中的数字相乘 到目前为止,我已经做到了以下几点,但b的值在文档中显示为11000英镑,当我将其发送出去时,它总是将其返回为0 我想把b的值设为11000,这样我就可以乘法,任何想法或已经编写的代码都会很好Vba Word文档中的发票,vba,excel,ms-word,Vba,Excel,Ms Word,我正在尝试编写一个VBA脚本,以便将ms word中表格中的数字相乘 到目前为止,我已经做到了以下几点,但b的值在文档中显示为11000英镑,当我将其发送出去时,它总是将其返回为0 我想把b的值设为11000,这样我就可以乘法,任何想法或已经编写的代码都会很好 Private Sub Calculate_Click() Dim t1 As Table Dim a As Double Dim m, b As Currency ' first table
Private Sub Calculate_Click()
Dim t1 As Table
Dim a As Double
Dim m, b As Currency
' first table
Set t1 = ActiveDocument.Tables(1)
a = Val(t1.Cell(5, 3).Range.Text)
a = a/100
b = Val(t1.Cell(7, 4).Range.Text)
'b = CCur(t1.Cell(7, 4).Range.Text) **I tried this but didn't work
MsgBox b
m = a * b
t1.Cell(5, 4).Range.Text = Format(m, "Currency")
End Sub
您的代码是完美的-直到您尝试在蛋糕上添加糖霜为止。:-)
将a变暗为双精度,将b变暗为双精度
这是因为您不想将苹果与橙子相乘。
m=a*b
给出了正确的结果,但转换为基本上除以100的货币。但这可能是在这一行中完成的,t1.Cell(5,4).range.Text=Format(m,“Currency”)
。我不知道。
我建议将m声明为Double,并将结果格式化为format(m,“#,##0.00”)
下面是完整的建议代码
Private Sub Calculate_Click()
Dim a As Double, b As Double, m As Double
Dim Ccy As String
With ActiveDocument.Tables(1) ' first table
a = CellValue(.Cell(5, 3))
b = CellValue(.Cell(7, 4), Ccy)
m = a * b / 100
If Ccy = "" Then Ccy = ChrW(163)
If Len(Ccy) > 1 Then Ccy = Ccy & " "
.Cell(5, 4).range.Text = Format(m, Ccy & "#,##0.00")
' .Cell(5, 4).range.Text = Format(m, "Currency")
End With
End Sub
Private Function CellValue(Target As Cell, _
Optional Ccy As String) As Double
Dim Numbers As String
Dim Txt As String, Ch As String
Dim i As Long, n As Long
Txt = Replace(Trim(Target.range.Text), Chr(13), "")
n = Len(Txt)
Do While i < n
i = i + 1
Ch = Mid(Txt, i, 1)
If IsNumeric(Ch) Then Exit Do
Loop
If i Then Ccy = Trim(Left(Txt, i - 1))
Txt = Mid(Txt, i)
n = Len(Txt)
For i = 1 To n
Ch = Mid(Txt, i, 1)
If IsNumeric(Ch) Or (Ch = ".") Then Numbers = Numbers & Ch
Next i
CellValue = Val(Numbers)
End Function
Private子计算\u Click()
调暗a为双色,b为双色,m为双色
像线一样模糊
使用ActiveDocument.Tables(1)的第一个表
a=单元值(.Cell(5,3))
b=单元值(.Cell(7,4),Ccy)
m=a*b/100
如果Ccy=”“,则Ccy=ChrW(163)
如果Len(Ccy)>1,则Ccy=Ccy&“
.Cell(5,4).range.Text=格式(m,Ccy&“#,##0.00”)
'.Cell(5,4).range.Text=格式(m,“货币”)
以
端接头
专用函数CellValue(目标为单元格_
可选Ccy(作为字符串)作为双精度
将数字变暗为字符串
Dim Txt作为字符串,Ch作为字符串
暗淡的i一样长,n一样长
Txt=Replace(Trim(Target.range.Text),Chr(13),“”)
n=Len(Txt)
趁我不在的时候做
i=i+1
Ch=Mid(Txt,i,1)
如果是数字(Ch),则退出Do
环
如果i那么Ccy=Trim(左(Txt,i-1))
Txt=Mid(Txt,i)
n=Len(Txt)
对于i=1到n
Ch=Mid(Txt,i,1)
如果是数字(Ch)或(Ch=“.”),则数字=数字和Ch
接下来我
CellValue=Val(数字)
端函数
ChrW(163)是符号。我已将其添加到输出的数字格式中。使用此行或使用“货币”作为格式的行之间的区别在于,“货币”格式是在“区域设置”中定义的,而另一行是在您看到它的地方定义的
函数
CellValue
将拾取价格单元格中使用的货币,并将其应用于总单元格。如果货币是多字符缩写(如GBP),则会添加空格。如果未指定货币,则将使用英镑,而不使用空格。您的代码非常完美-直到您尝试在蛋糕上加冰为止。:-)
将a变暗为双精度,将b变暗为双精度
这是因为您不想将苹果与橙子相乘。
m=a*b
给出了正确的结果,但是转换成了基本上除以100的货币。但这可能是在这一行中完成的,t1.Cell(5,4.range.Text=Format(m,“货币”)
。我不知道。
我建议将m声明为Double,并将结果格式化为format(m,“#,##0.00”)
下面是完整的建议代码
Private Sub Calculate_Click()
Dim a As Double, b As Double, m As Double
Dim Ccy As String
With ActiveDocument.Tables(1) ' first table
a = CellValue(.Cell(5, 3))
b = CellValue(.Cell(7, 4), Ccy)
m = a * b / 100
If Ccy = "" Then Ccy = ChrW(163)
If Len(Ccy) > 1 Then Ccy = Ccy & " "
.Cell(5, 4).range.Text = Format(m, Ccy & "#,##0.00")
' .Cell(5, 4).range.Text = Format(m, "Currency")
End With
End Sub
Private Function CellValue(Target As Cell, _
Optional Ccy As String) As Double
Dim Numbers As String
Dim Txt As String, Ch As String
Dim i As Long, n As Long
Txt = Replace(Trim(Target.range.Text), Chr(13), "")
n = Len(Txt)
Do While i < n
i = i + 1
Ch = Mid(Txt, i, 1)
If IsNumeric(Ch) Then Exit Do
Loop
If i Then Ccy = Trim(Left(Txt, i - 1))
Txt = Mid(Txt, i)
n = Len(Txt)
For i = 1 To n
Ch = Mid(Txt, i, 1)
If IsNumeric(Ch) Or (Ch = ".") Then Numbers = Numbers & Ch
Next i
CellValue = Val(Numbers)
End Function
Private子计算\u Click()
调暗a为双色,b为双色,m为双色
像线一样模糊
使用ActiveDocument.Tables(1)的第一个表
a=单元值(.Cell(5,3))
b=单元值(.Cell(7,4),Ccy)
m=a*b/100
如果Ccy=”“,则Ccy=ChrW(163)
如果Len(Ccy)>1,则Ccy=Ccy&“
.Cell(5,4).range.Text=格式(m,Ccy&“#,##0.00”)
'.Cell(5,4).range.Text=格式(m,“货币”)
以
端接头
专用函数CellValue(目标为单元格_
可选Ccy(作为字符串)作为双精度
将数字变暗为字符串
Dim Txt作为字符串,Ch作为字符串
暗淡的i一样长,n一样长
Txt=Replace(Trim(Target.range.Text),Chr(13),“”)
n=Len(Txt)
趁我不在的时候做
i=i+1
Ch=Mid(Txt,i,1)
如果是数字(Ch),则退出Do
环
如果i那么Ccy=Trim(左(Txt,i-1))
Txt=Mid(Txt,i)
n=Len(Txt)
对于i=1到n
Ch=Mid(Txt,i,1)
如果是数字(Ch)或(Ch=“.”),则数字=数字和Ch
接下来我
CellValue=Val(数字)
端函数
ChrW(163)是英镑符号。我已将其添加到输出的数字格式中。使用此行或使用“货币”作为格式的行之间的区别在于,“货币”格式在“区域设置”中定义,而另一行在您看到它的地方定义
函数
CellValue
将提取价格单元格中使用的货币,并将其应用于总单元格。如果货币是多字符缩写(如GBP),将添加空格。如果没有货币,将使用英镑,没有空格。实际上这是一个有趣的问题。一般来说,如果您想将70066欧元和52美分乘以10,则如下所示:
在我的设置中,我使用,
作为十进制分隔符,
作为千位分隔符。因此,我将,
替换为
,并在开始时忽略
。像这样:
Replace(Replace(.Cell(1, 1).Range.Text, ".", ""), ",", ".")
整个代码如下所示:
Option Explicit
Private Sub TestMe()
Dim t1 As Table
Dim a As Currency
Dim m As Currency
Dim b As Currency
Set t1 = ActiveDocument.Tables(1)
With t1
a = Val(Replace(Replace(.Cell(1, 1).Range.Text, ".", ""), ",", "."))
b = Val(Replace(Replace(.Cell(1, 2).Range.Text, ".", ""), ",", "."))
m = a * b
.Cell(2, 1).Range.Text = Format(m, "Currency")
End With
End Sub
它将70066,52欧元乘以10,返回700665,20欧元。实际上这是一个有趣的问题。一般来说,如果您想将70066欧元和52美分乘以10,则如下所示: 在我的设置中,我使用
,
作为十进制分隔符,
作为千位分隔符。因此,我将,
替换为
,并在开始时忽略
。像这样:
Replace(Replace(.Cell(1, 1).Range.Text, ".", ""), ",", ".")
整个代码如下所示:
Option Explicit
Private Sub TestMe()
Dim t1 As Table
Dim a As Currency
Dim m As Currency
Dim b As Currency
Set t1 = ActiveDocument.Tables(1)
With t1
a = Val(Replace(Replace(.Cell(1, 1).Range.Text, ".", ""), ",", "."))
b = Val(Replace(Replace(.Cell(1, 2).Range.Text, ".", ""), ",", "."))
m = a * b
.Cell(2, 1).Range.Text = Format(m, "Currency")
End With
End Sub
它将70066,52欧元乘以10,返回700665,20欧元。m未定义为货币,我认为您认为它是Val。如果单元格文本被格式化,您可能需要替换逗号和货币符号。如何获取