Sub RR()
Dim ws As Worksheet
Dim uploaderWs As Worksheet
Dim empWs As Worksheet
Dim settingsWs As Worksheet
Dim i As Long, j As Long, uploaderRow As Long
Dim randValue As Long
Dim currentTime As Date
Set ws = ActiveSheet ' Change to your sheet if needed: ThisWorkbook.Sheets("SheetName")
Set uploaderWs = ThisWorkbook.Sheets("uploader")
Set empWs = ThisWorkbook.Sheets("EMP")
Set settingsWs = ThisWorkbook.Sheets("settings")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
currentTime = Now
ws.Range("A2:E321").ClearContents
For i = 2 To 321 Step 8
uploaderRow = ((i - 2) \ 8) + 2 ' Calculate corresponding uploader row
If uploaderWs.Cells(uploaderRow, 1).Value = "" Then
' Leave empty if uploader cell is blank
ws.Range("A" & i & ":E" & i + 7).Value = ""
Else
For j = 0 To 7
' Column A: Random number
Randomize
randValue = Int((2147483647 - (-2147483648#) + 1) * Rnd + (-2147483648#))
ws.Cells(i + j, 1).Value = randValue
' Column B: EMP value
ws.Cells(i + j, 2).Value = empWs.Cells(uploaderRow, 1).Value
' Column C: Sequence from 1 to 8
ws.Cells(i + j, 3).Value = j + 1
' Column D: Complex logic
Select Case j
Case 0 ' First row in block (D2, D10, etc.)
ws.Cells(i + j, 4).Value = uploaderWs.Cells(uploaderRow, 5).Value - 1
Case 1 ' Second row in block (D3, D11, etc.)
If uploaderWs.Cells(uploaderRow, 6).Value = settingsWs.Range("T3").Value Then
ws.Cells(i + j, 4).Value = "0"
ElseIf uploaderWs.Cells(uploaderRow, 6).Value = settingsWs.Range("T4").Value Then
ws.Cells(i + j, 4).Value = "1"
Else
ws.Cells(i + j, 4).Value = "2"
End If
Case 2 ' Third row in block (D4, D12, etc.)
If uploaderWs.Cells(uploaderRow, 7).Value = settingsWs.Range("W3").Value Then
ws.Cells(i + j, 4).Value = "0"
Else
ws.Cells(i + j, 4).Value = "1"
End If
Case 3 ' Fourth row in block (D5, D13, etc.)
ws.Cells(i + j, 4).Value = uploaderWs.Cells(uploaderRow, 8).Value - 1
Case 4 ' Fifth row in block (D6, D14, etc.)
ws.Cells(i + j, 4).Value = uploaderWs.Cells(uploaderRow, 9).Value - 1
Case 5 ' Sixth row in block (D7, D15, etc.)
ws.Cells(i + j, 4).Value = uploaderWs.Cells(uploaderRow, 10).Value - 1
Case 6 ' Seventh row in block (D8, D16, etc.)
ws.Cells(i + j, 4).Value = uploaderWs.Cells(uploaderRow, 11).Value - 1
Case 7 ' Eighth row in block (D9, D17, etc.)
ws.Cells(i + j, 4).Value = uploaderWs.Cells(uploaderRow, 12).Value - 1
End Select
' Column E: Current timestamp
ws.Cells(i + j, 5).Value = currentTime
Next j
End If
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
msgbox "RR is OK", vbInformation
End Sub