MarkVMcCullagh
Board Regular
- Joined
- Oct 22, 2002
- Messages
- 72
I have a worksheet that contains date/time data in Columns A-D
I have managed to load these into a VB static array. Once some calculations have been done the answers are posted back to elsewhere on the worksheet using a second array.
What I would like to do is generate a dynamic array that matches the number of rows of data involved. I am speaking about the first array ie. firstrange. The reason I want to convert this to dynamic rather than static is that it is more efficient and hopefully it won't crash when it encounters a row without data.
For your info columns A and C contain dates and columns B and D contain dates. Basically what I am doing is (C+D)-(A+B) for each row.
This is my first attempt at VB so I know my code will look rough.
Sub CategoriseCalls()
Dim firstrange As Variant
Dim secondrange As Variant
Dim i As Long
Dim VarMY As Date
firstrange = Range("a2:d65532").Value ' Select all cells in Column a-d
ReDim secondrange(1 To UBound(firstrange, 1), 1 To 2)
Var4 = 4 / 24 '4 hours
Var6 = 6 / 24 '6 hours
Var8 = 8 / 24 '8 hours
For i = 1 To UBound(firstrange, 1)
If (firstrange(i, 4) + firstrange(i, 3)) - (firstrange(i, 2) + firstrange(i, 1)) <= Var4 Then
secondrange(i, 1) = 4
Else
If (firstrange(i, 4) + firstrange(i, 3)) - (firstrange(i, 2) + firstrange(i, 1)) <= Var6 Then
secondrange(i, 1) = 6
Else
If (firstrange(i, 4) + firstrange(i, 3)) - (firstrange(i, 2) + firstrange(i, 1)) <= Var8 Then
secondrange(i, 1) = 8
Else
secondrange(i, 1) = 9
End If
End If
End If
VarMY = DateValue(Month(firstrange(i, 1)) & "/" & Year(firstrange(i, 1)))
secondrange(i, 2) = VarMY
Next i
Range("j2").Resize(UBound(firstrange, 1), 2).Value = secondrange
MsgBox "Completed"
End Sub
I have managed to load these into a VB static array. Once some calculations have been done the answers are posted back to elsewhere on the worksheet using a second array.
What I would like to do is generate a dynamic array that matches the number of rows of data involved. I am speaking about the first array ie. firstrange. The reason I want to convert this to dynamic rather than static is that it is more efficient and hopefully it won't crash when it encounters a row without data.
For your info columns A and C contain dates and columns B and D contain dates. Basically what I am doing is (C+D)-(A+B) for each row.
This is my first attempt at VB so I know my code will look rough.
Sub CategoriseCalls()
Dim firstrange As Variant
Dim secondrange As Variant
Dim i As Long
Dim VarMY As Date
firstrange = Range("a2:d65532").Value ' Select all cells in Column a-d
ReDim secondrange(1 To UBound(firstrange, 1), 1 To 2)
Var4 = 4 / 24 '4 hours
Var6 = 6 / 24 '6 hours
Var8 = 8 / 24 '8 hours
For i = 1 To UBound(firstrange, 1)
If (firstrange(i, 4) + firstrange(i, 3)) - (firstrange(i, 2) + firstrange(i, 1)) <= Var4 Then
secondrange(i, 1) = 4
Else
If (firstrange(i, 4) + firstrange(i, 3)) - (firstrange(i, 2) + firstrange(i, 1)) <= Var6 Then
secondrange(i, 1) = 6
Else
If (firstrange(i, 4) + firstrange(i, 3)) - (firstrange(i, 2) + firstrange(i, 1)) <= Var8 Then
secondrange(i, 1) = 8
Else
secondrange(i, 1) = 9
End If
End If
End If
VarMY = DateValue(Month(firstrange(i, 1)) & "/" & Year(firstrange(i, 1)))
secondrange(i, 2) = VarMY
Next i
Range("j2").Resize(UBound(firstrange, 1), 2).Value = secondrange
MsgBox "Completed"
End Sub