VBA问题:简单的操作与多种types的variables

在处理VBA函数中的Excel 2010中不同types的variables时,我遇到了一个非常烦人的问题。 我已经尝试了以下代码与广告没有Excel转换感谢关于同样的溢出问题(错误6)的其他问题,但它没有解决(我可能做错了,但我没有我的指示了)

Function do_calculation(lig_desti As Integer, col_desti As Integer) Range(Cells(lig_desti, col_desti), Cells(lig_desti, col_desti)).Value = Range(Cells(lig_desti, col_desti - 3), Cells(lig_desti, col_desti - 3)).Value * (Range(Cells(lig_desti, col_desti - 1), Cells(lig_desti, col_desti - 1)).Value / Range(Cells(lig_desti, col_desti - 2), Cells(lig_desti, col_desti - 2)).Value) End Function 

只是总结,我想要做的(对于每一行)D = A *(C / B)。

这个想法是,我有3列值,这是第一行只是给你一个想法:

 ABCD 2187 71,8 18 1050 78,65 18 1813 92,35 18 2379,45 94 18 

所以基本上(除非我错了),我必须处理长,双打和整数。 请注意,“C”列将始终包含相同的值(18),A列可能包含大于20k或30k的大整数

在此先感谢您的帮助

也许试试这个

 Sub WriteFormula(ByVal sh As Excel.Worksheet, ByVal lRow As Long, ByVal lColumn As Long) Dim rng As Excel.Range Set rng = sh.Cells(lRow, lColumn) rng.FormulaR1C1 = "=RC[-3]*(RC[-1]/RC[-2])" End Sub Sub Test() WriteFormula ActiveSheet, 1, 4 End Sub 

要以简单的方式执行此操作,可以使用Application.Caller 。 要做到这一点,它也可以作为数组使用Evaluate

把这个成**模块*:

 Public Function do_calculation() As Variant With Application.Caller '.Cells '(1, 1) do_calculation = Evaluate(.Offset(, -3).Address & "*" & .Offset(, -1).Address & "/" & .Offset(, -2).Address) End With End Function 

然后在D1中input=do_calculation()并复制下来。 或者select范围D1:D4并input=do_calculation()但用Ctrl + Shift + Enter确认

直接获取值,你需要通过一个Sub这样做:

 Sub do_calculation(rng As Range) With rng .Value = Evaluate(.Offset(, -3).Address & "*" & .Offset(, -1).Address & "/" & .Offset(, -2).Address) End With End Sub 

只需select单元格D1:D4并运行do_calculation selection