jeudi 30 juillet 2015

Logic - within multiple for loops and dynamic arrays

Third day programming VBA. I have generated this code and wanted someone to run an eye over it as it is not having the intended affect.

The idea is to pull data from a MS Project export in one sheet and reproduce it as a schedule in another.

The code works I just have a logic error (I am guessing somewhere within my array). It is also very time consuming as it has a triple For loop. I can't think of a way to remove this so if anyone has any ideas let me know!

Private Sub CommandButton1_Click()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim n As Integer
Dim p As Integer

Dim Arr() As Variant

For j = 6 To 29
k = 1
For i = 1 To 1000
    ReDim Preserve Arr(1 To 3, k)
    If ThisWorkbook.Worksheets("Sheet2").Cells(j, 1).Value = ThisWorkbook.Worksheets("Sheet1").Cells(i, 10) Then
    Arr(1, k) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 4).Value
    Arr(2, k) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 6).Value
    Arr(3, k) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 7).Value
    k = k + 1
    End If
Next i

k = k - 1

For p = 1 To k
    m = 1
    For i = 3 To 253
        n = i + 1
        If Arr(2, m) <= ThisWorkbook.Worksheets("Sheet1").Cells(i, 6).Value And Arr(3, m) >= ThisWorkbook.Worksheets("Sheet1").Cells(n, 6).Value Then
        ThisWorkbook.Worksheets("Sheet2").Cells(j, i).Value = Arr(1, m)
        End If
    Next i
    m = m + 1
Next p
ReDim Arr(1 To 3, k)
Next j
End Sub

Aucun commentaire:

Enregistrer un commentaire