需要帮助LOOPING功能

时间:2013-11-05 23:56:30

标签: vba excel-vba excel

我的代码有问题......看起来像这样

Worksheets("Daily Consump").Cells(67, OutputNumber).Value = (CDbl(Kiln.Cells(25, OutputNumber + 3).Value) - CDbl(Kiln.Cells(25, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(25, 4).Value)
Worksheets("Daily Consump").Cells(68, OutputNumber).Value = (CDbl(Kiln.Cells(26, OutputNumber + 3).Value) - CDbl(Kiln.Cells(26, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(26, 4).Value)
Worksheets("Daily Consump").Cells(69, OutputNumber).Value = (CDbl(Kiln.Cells(27, OutputNumber + 3).Value) - CDbl(Kiln.Cells(27, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(27, 4).Value)
Worksheets("Daily Consump").Cells(70, OutputNumber).Value = (CDbl(Kiln.Cells(28, OutputNumber + 3).Value) - CDbl(Kiln.Cells(28, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(28, 4).Value)
Worksheets("Daily Consump").Cells(71, OutputNumber).Value = (CDbl(Kiln.Cells(29, OutputNumber + 3).Value) - CDbl(Kiln.Cells(29, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(29, 4).Value)
Worksheets("Daily Consump").Cells(72, OutputNumber).Value = (CDbl(Kiln.Cells(30, OutputNumber + 3).Value) - CDbl(Kiln.Cells(30, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(30, 4).Value)
Worksheets("Daily Consump").Cells(73, OutputNumber).Value = (CDbl(Kiln.Cells(31, OutputNumber + 3).Value) - CDbl(Kiln.Cells(31, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(31, 4).Value)

我认为我的代码不好,你有没有建议用简单的代码制作它,也许使用循环,但我有任何失败: 在这里我的试用

'For k = 25 To 31
'    For j = 67 To 73
'        Worksheets("Daily Consump").Cells(j, OutputNumber).Value = (CDbl(Kiln.Cells(k, OutputNumber + 3).Value) - CDbl(Kiln.Cells(k, OutputNumber + 2).Value)) * CDbl(Kiln.Cells(k, 4).Value)
'    Next j
'Next k

请告诉我该怎么做

2 个答案:

答案 0 :(得分:3)

With Kiln
    For k = 25 To 31
        Worksheets("Daily Consump").Cells(k + 42, OutputNumber).value = (CDbl(.Cells(k, OutputNumber + 3).value) - CDbl(.Cells(k, OutputNumber + 2).value)) * CDbl(.Cells(k, 4).value)
    Next k
End With

答案 1 :(得分:0)

我建议您不要在VBA中直接在Excel工作表中执行此操作。以下是如何制作公式的示例:

Formula

结果单元格获取值A和值B之间的差异,并乘以列Scale上的数字。要输入公式,您需要在选择结果单元格时按CTRL-SHIFT-ENTER(如上所示)。结果如下:

Result