首页 > 解决方案 > 根据比率将数字添加到范围

问题描述

对于背景故事,尝试根据每个小计与总费用的比率将固定费用添加到费用小计 (Y17:Y46)。尝试过,收效甚微。任何帮助,将不胜感激。Flatrefffee 和 Total 之前已在宏中分配了一个值。

Sub Flat_Fee()
'Adds in the flat referral fee, if it's a number
Dim cell As Range
Dim total As Integer
    For Each cell In Range("Y17:Y46").Select
        If IsNumeric(flatreffee) And cell.Value > 0 Then
            cell = cell + (cell / total) * flatreffee
        End If
    Next cell
End Sub

标签: excelvba

解决方案


' After first hard-coding total and flatrefee 
' and **removing ".Select"** this worked for me.
Sub Flat_Fee()
'Adds in the flat referral fee, if it's a number
Dim cell As Range
Dim total As Integer
    For Each cell In Range("Y17:Y46")
        If IsNumeric(flatreffee) And cell.Value > 0 Then
            cell = cell + (cell / total) * flatreffee
        End If
    Next cell
End Sub

推荐阅读