Home » excel » vba – Need help LOOPING function

vba – Need help LOOPING function

Posted by: admin May 14, 2020 Leave a comment

Questions:

i have problem with my code… look like this

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)

i think my code not good, do you any suggest to make it in simple code, maybe using looping, but i have any fail in that:
here my trial

'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

please advise me how to do that

How to&Answers:

I suggest instead of VBA to do this directly in Excel sheets. Here is an example of how to make the formulas:

Formula

The result cells take the difference between columns Value A and Value B and multiply by the numbers on column Scale. To enter the formula you need to press CTRL-SHIFT-ENTER while the result cells are selected (as seen above). The result is seen below:

Result

Answer:

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