动态范围

时间:2017-05-12 21:08:35

标签: vba dynamic range relative

我正在努力将变量实现到具有相对引用的公式中。不假定行数是固定的。该公式应该计算在计算完成的单元格左侧的列中的份额(值列)除以值列中的总计。 查找单元格的参考值和值的总和,由宏的另一部分完成 - 该部分正在工作。

Cells.Find(What:="Cost name", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True).Activate
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(1, 0).Range("A1").Select
intRowStart = ActiveCell.Row

'identify the number of rows by non-empty cells in a Cost name column
Selection.End(xlDown).Select
intRowEnd = ActiveCell.Row
intRowsNumber = intRowEnd - intRowStart + 1

Cells.Find(What:="Cost name", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True).Activate
Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Set rngTotal = Selection
ActiveCell.Offset(1, 0).Range("A1").Select

 'find the total cost value: searched for in the same way 
 'as the with `the above Find - with xldown (last non-empty cell) 
 'and offset (1,6) to the right to the Total of Cost value column
 dblTotal = ActiveCell.Value 'immediate shows that correct value is passed

'approach 1
ActiveCell.FormulaR1C1 = "=RC[-1]/SUM(" & rngTotal & ")"
'approach 2
Selection.FormulaR1C1 = "=RC[-1] /" & dblTotal
'approach 3
Selection.FormulaR1C1 = "=RC[-1] / SUM(R[-" & CStr(intRowsNumber + 1) & "]C:R[-1]C" 

任何人都可以帮我解决这些问题吗?这是语法问题吗?

安娜

0 个答案:

没有答案