Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50228

[SOLVED] Help with calculation loop

$
0
0
Hi, I'm trying to carry out this calculation, I know the loops I need but I am not familiar enough with vba to write it. This is what I want to do:
Select cell 1, find next value in column as cell 2 (there are empty cells in between). Take all empty cell rows between 1 and 2 and add values from other column, lets call this c. Write 1-2+c in the row of 1. I've attached a sample file that will make a lot more sense, I know this won't make that much. This is the loop I have so far, it doesn't go to the next row properly, feel free to modify or rewrite with a new format if you have an idea on how to do this. (Don't run the code, it gets stuck in a loop).

Code:

Private Sub UseCalc()
    Dim ws As Worksheet
    Dim i As Long
    Dim lr As Long
    Dim a As Long
    Dim u1 As Long
    Dim u2 As Long
    Dim use As Long
    Dim order As Long
    Dim D1 As Date
    Dim D2 As Date
    Dim day As Long
       
   
    Set ws = Sheets("RawData")
        With ws
            lr = .ListObjects("RefTable1").ListRows.Count
            MsgBox lr
            If lr >= 5 Then
                For a = 1 To lr
                    b = a + 1
                    order = 0
                    .ListObjects("RefTable" & "1").DataBodyRange(a, 4).Select
                    u1 = ActiveCell.Value
                    D1 = ActiveCell.Offset(0, -1).Value
                        Do While .ListObjects("RefTable1").Range(b, 4) = ""
                            order = .ListObjects("RefTable" & "1").Range(b + 1, 5) + order
                            b = b + 1
                        Loop
                    u2 = .ListObjects("RefTable" & "1").Range(b, 4).Value
                    D2 = .ListObjects("RefTable" & "1").Range(b, 3).Value
                    use = u1 - u2 + order
                    day = D2 - D1
                    .ListObjects("RefTable" & "1").DataBodyRange(a, 6).Value = use
                    .ListObjects("RefTable" & "1").DataBodyRange(a, 7).Value = day
                Next a
            End If
        End With
                   
End Sub

Attached Files

Viewing all articles
Browse latest Browse all 50228

Trending Articles