VBA除以最后值

时间:2018-10-23 15:12:03

标签: excel vba excel-vba

我有这样的桌子

Column A    Column B
 5            25
 4            20
 8            40
 3            15
20           100

enter image description here

我想使用vba脚本从A列中选择最后一个值,以像=A1/last Value Form coumn A*100一样进行划分

3 个答案:

答案 0 :(得分:1)

将此内容放入B1并复制下来。

=A1/LOOKUP(2,1/A:A,A:A)*100

enter image description here

答案 1 :(得分:1)

如果您正在Excel-VBA中寻找解决方案,那么就在这里。

Sub test()

Dim result As Variant
Dim firstvalue As Variant
Dim secondvalue As Variant
firstvalue = Range("A1").Value
secondvalue = Range("A" & Cells(Rows.Count, 2).End(xlUp).Row).Value
result = (firstvalue / secondvalue) * 100

End Sub

答案 2 :(得分:0)

使用VBA。它使用一个快速的数组,并完成整个列,如图所示。

Option Explicit
Public Sub DivideByLastValue()
    Dim lastRow As Long, arr(), i As Long, divisor As Double
    With ThisWorkbook.Worksheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        arr = .Range("A1:B" & lastRow).Value
        divisor = arr(UBound(arr, 1), 1)
        For i = LBound(arr, 1) To UBound(arr, 1)
            arr(i, 2) = arr(i, 1) / divisor * 100
        Next
        .Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    End With
End Sub