我有列指数,如VENTA,TOTAL,VACUNA(销售,总计,疫苗),最后总计每行(3),我希望自动生成总数,但是数据收费的方式不是传统的,如下所示:
这就是为什么我需要创建一个VBA脚本,无论什么时候出售我需要根据最后的总数(3)自动更新总计每次行(3)我有(全部或疫苗)
这是我创建的代码(我知道它真的是硬编码的),我有编译错误:
{{1}}
或者您认为有更简单的方法来完成这项任务吗?
编辑:我认为如果我要求最后一个VENTA,TOTAL或VACUNA的列并且根据它进行减法会更容易。有办法做到这一点吗? 还有代码,因为它我已经意识到这是错误的,因为VACUNA上可能有一些空单元格,总数会显示0但是对于那个特定的单元格,它必须是该行的最后一个TOTAL。
答案 0 :(得分:0)
我使用更清晰的第二种方法解决了我的问题
Function LastTotal() As Variant
Dim ultima As Long, v As Long
ultima = Cells(2, Columns.Count).End(xlToLeft).Column
For v = ultima - 2 To 1 Step -1
If Cells(2, v).Value = "TOTAL" Then
LastTotal = v
Exit For
End If
Next v
End Function
Function LastVacuna() As Variant
Dim ultima As Long, v As Long
ultima = Cells(2, Columns.Count).End(xlToLeft).Column
For v = ultima - 2 To 1 Step -1
If Cells(2, v).Value = "VACUNA" Then
LastVacuna = v
Exit For
End If
Next v
End Function
Function LastVenta() As Variant
Dim ultima As Long, v As Long
ultima = Cells(2, Columns.Count).End(xlToLeft).Column
For v = ultima - 2 To 1 Step -1
If Cells(2, v).Value = "VENTA" Then
LastVenta = v
Exit For
End If
Next v
End Function
Public Sub CalculoTotal()
Dim ultima As Long, venta As Long, vacuna As Long, total As Long, ventas(5 To 10) As Long, c As Long
ultima = Cells(2, Columns.Count).End(xlToLeft).Column
venta = LastVenta()
vacuna = LastVacuna()
total = LastTotal()
If total > vacuna And total > venta Then
For c = 5 To 10
Cells(c, ultima).Value = Cells(c, total).Value
Next c
ElseIf venta > total And total > vacuna Then
For c = 5 To 10
Cells(c, ultima).Value = Cells(c, total).Value + Cells(c, venta).Value
Next c
ElseIf venta > vacuna And vacuna > total Then
For c = 5 To 10
If Cells(c, vacuna) = "" Then
Cells(c, ultima).Value = Cells(c, total).Value + Cells(c, venta).Value
Else
Cells(c, ultima).Value = Cells(c, vacuna).Value + Cells(c, venta).Value
End If
Next c
ElseIf vacuna > total And total > venta Then
For c = 5 To 10
If Cells(c, vacuna) = "" Then
Cells(c, ultima).Value = Cells(c, total).Value
Else
Cells(c, ultima).Value = Cells(c, vacuna).Value
End If
Next c
ElseIf vacuna > total And venta > total Then
For c = 5 To 10
If Cells(c, vacuna) = "" Then
Cells(c, ultima).Value = Cells(c, total).Value + Cells(c, venta).Value
Else
Cells(c, ultima).Value = Cells(c, vacuna).Value + Cells(c, vacuna).Value
End If
Next c
End If
End Sub