Calculating each row separately

pauloalex

New Member
Joined
Jan 16, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I'm trying to create a sheet with some intervals in each row. The total of sums of the intervals of each row must be 08:00. But I'm having problems, because the excel doesn't calculate each row at time, but every row simultaneously, and the total in each row ends in a value different of 08:00.
Here my source:
VBA Code:
Sub Macro1()

Range("C2:G32").Select
Selection.ClearContents


For Each linha In Range("A2:A32")

i = linha.Row

If Range("B" & i) = "Saturday" Then

Range("C" & i).Value = " "
Range("D" & i).Value = " "
Range("E" & i).Value = " "
Range("F" & i).Value = " "
Range("G" & i).Value = " "

ElseIf Range("B" & i) = "Sunday" Then

Range("C" & i).Value = " "
Range("D" & i).Value = " "
Range("E" & i).Value = " "
Range("F" & i).Value = " "
Range("G" & i).Value = " "

Else

While (Range("G" & i) <> "08:00")

Range("C" & i).Value = "=TEXT(RAND()*(TIME(8,16,0)-TIME(7,45,0))+TIME(7,45,0),""HH:MM"")"
Range("D" & i).Value = "=TEXT(RAND()*(TIME(12,16,0)-TIME(11,45,0))+TIME(11,45,0),""HH:MM"")"
Range("E" & i).Value = "=TEXT(RAND()*(TIME(13,46,0)-TIME(13,15,0))+TIME(13,15,0),""HH:MM"")"
Range("F" & i).Value = "=TEXT(RAND()*(TIME(17,46,0)-TIME(17,15,0))+TIME(17,15,0),""HH:MM"")"
Range("G" & i).Value = "=TEXT((D" & i & " - C" & i & ") + (F" & i & " - E" & i & "),""HH:MM"")"

Wend

End If

Next

End Sub

Here my file:
Month.xlsm
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You need to get rid of the formulas in the cells after updated them. Change the last part of your macro like this:
VBA Code:
            ...
            While (Cells(i, 7) <> "08:00")
                Range("C" & i).Value = "=TEXT(RAND()*(TIME(8,16,0)-TIME(7,45,0))+TIME(7,45,0),""HH:MM"")"
                Range("D" & i).Value = "=TEXT(RAND()*(TIME(12,16,0)-TIME(11,45,0))+TIME(11,45,0),""HH:MM"")"
                Range("E" & i).Value = "=TEXT(RAND()*(TIME(13,46,0)-TIME(13,15,0))+TIME(13,15,0),""HH:MM"")"
                Range("F" & i).Value = "=TEXT(RAND()*(TIME(17,46,0)-TIME(17,15,0))+TIME(17,15,0),""HH:MM"")"
                Range("G" & i).Value = "=TEXT((D" & i & " - C" & i & ") + (F" & i & " - E" & i & "),""HH:MM"")"
                Range("C" & i & ":G" & i).Copy
                Range("C" & i).PasteSpecial Paste:=xlPasteValues
            Wend
        End If
    Next
    Application.CutCopyMode = False
    Range("A1").Select

End Sub
 
Last edited:
Upvote 0
Solution
You need to get rid of the formulas in the cells after updated them. Change the last part of your macro like this:
VBA Code:
            ...
            While (Cells(i, 7) <> "08:00")
                Range("C" & i).Value = "=TEXT(RAND()*(TIME(8,16,0)-TIME(7,45,0))+TIME(7,45,0),""HH:MM"")"
                Range("D" & i).Value = "=TEXT(RAND()*(TIME(12,16,0)-TIME(11,45,0))+TIME(11,45,0),""HH:MM"")"
                Range("E" & i).Value = "=TEXT(RAND()*(TIME(13,46,0)-TIME(13,15,0))+TIME(13,15,0),""HH:MM"")"
                Range("F" & i).Value = "=TEXT(RAND()*(TIME(17,46,0)-TIME(17,15,0))+TIME(17,15,0),""HH:MM"")"
                Range("G" & i).Value = "=TEXT((D" & i & " - C" & i & ") + (F" & i & " - E" & i & "),""HH:MM"")"
                Range("C" & i & ":G" & i).Copy
                Range("C" & i).PasteSpecial Paste:=xlPasteValues
            Wend
        End If
    Next
    Application.CutCopyMode = False
    Range("A1").Select

End Sub
Wooooow Man!

You are awesome! You solve the problem despite my bad English ? Thank you very much!!

You are Awesome!!
 
Upvote 0
I'm glad I've been of some help(y).
... and like this will be much faster:
VBA Code:
        ...
        Else
            Application.ScreenUpdating = False
            While (Cells(i, 7) <> "08:00")
                Range("C" & i).Value = "=TEXT(RAND()*(TIME(8,16,0)-TIME(7,45,0))+TIME(7,45,0),""HH:MM"")"
                Range("D" & i).Value = "=TEXT(RAND()*(TIME(12,16,0)-TIME(11,45,0))+TIME(11,45,0),""HH:MM"")"
                Range("E" & i).Value = "=TEXT(RAND()*(TIME(13,46,0)-TIME(13,15,0))+TIME(13,15,0),""HH:MM"")"
                Range("F" & i).Value = "=TEXT(RAND()*(TIME(17,46,0)-TIME(17,15,0))+TIME(17,15,0),""HH:MM"")"
                Range("G" & i).Value = "=TEXT((D" & i & " - C" & i & ") + (F" & i & " - E" & i & "),""HH:MM"")"
            Wend
            Application.ScreenUpdating = True
            Range("C" & i & ":G" & i).Copy
            Range("C" & i).PasteSpecial Paste:=xlPasteValues
        End If
    Next
    Application.CutCopyMode = False
    Range("A1").Select
  
End Sub
 
Upvote 0
If you write the values straight to the cells instead of the formulas then you don't need to copy and paste the values afterwards.
VBA Code:
        ...
        Else
            Application.ScreenUpdating = False
            While (Cells(i, 7) <> "08:00")
                Range("C" & i).Value = Evaluate("=TEXT(RAND()*(TIME(8,16,0)-TIME(7,45,0))+TIME(7,45,0),""HH:MM"")")
                Range("D" & i).Value = Evaluate("=TEXT(RAND()*(TIME(12,16,0)-TIME(11,45,0))+TIME(11,45,0),""HH:MM"")")
                Range("E" & i).Value = Evaluate("=TEXT(RAND()*(TIME(13,46,0)-TIME(13,15,0))+TIME(13,15,0),""HH:MM"")")
                Range("F" & i).Value = Evaluate("=TEXT(RAND()*(TIME(17,46,0)-TIME(17,15,0))+TIME(17,15,0),""HH:MM"")")
                Range("G" & i).Value = Evaluate("=TEXT((D" & i & " - C" & i & ") + (F" & i & " - E" & i & "),""HH:MM"")")
            Wend
            Application.ScreenUpdating = True
        End If
    Next
End Sub

On a different note, looking at the rest of the code in the first post, it would be better not to have a space between the double quotes for the cells that you want to be blank.
The preferable method would be to clear contents on those cells with vba or to use double quotes with no space between them in a formula, that way you are less likely to run into problems later if you try to evaluate those cells as blank or empty.
 
Upvote 0
Yes @jasonb75, I keep forgetting the function Evalute.
But in this specific case I would not use it for column "G" because While (Cells(i, 7) <> "08:00") will no longer work since the cell will format HH:MM:SS causing the macro to loop.
 
Upvote 0
@rollis13
I had missed the fact that the last cell being processed was the trigger for the loop.
With that in mind, I would say that it would be better to use your method so that all of the formulas are able to recalculate until the loop ends otherwise the values will never change and the loop will be infinite.
 
Upvote 0
@jasonb, it will also work as you suggested in your post #5 without the Evalute on the "G" column.
 
Upvote 0
Ever had one of those days where 1+1=6 no matter how many times you do the math?

I was thinking it wouldn't work because it would need to recalculate, not re-evaluate :oops:

Yeah, I think I'm gonna keep quiet now, sneak out the back and hope nobody noticed I was here...
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top