1

This code is working fine but it has minor defect. I was hoping to get some help here.

This code needs to compare 2 values and divide the value in equal parts and place it in next cell.

First 2 conditions are working fine. The third condition is working fine but has 2 issues mentioned below which I need help with.

  1. For example if X = 2 and Y = 8, it should divide Y in 4 equal parts as per X value but it is only placing 3 values of 2 in offset cells
  2. Also, if Y = 7 then it should place values as 2 2 2 1 in corresponding cells
  3. While it is doing the work for first cell having Y > X, it is putting incorrect value in farther cell for next Y > X value

Please advise on what needs to be changed.

Sub Calc()
    Dim ws As Worksheet
    Dim i, j, x, y As Variant
    Dim lrow As Long
    lrow = Worksheets("AB").Cells(Rows.Count, 1).End(xlUp).Row
    Set ws = Workbooks("BC.xlsm").Worksheets("AB")
    j = 9

    With ws
        .Activate
        For i = 2 To lrow
            x = Cells(i, 7).Value
            y = Cells(i, 8).Value
            If y < 0 Then
                Cells(i, 8).Offset(0, 1) = y
            ElseIf y <= x Then
                Cells(i, 8).Offset(0, 1) = y
            ElseIf y > x Then
                Do Until y <= x
                    Cells(i, j) = x
                    y = y - x
                    j = j + 1
                Loop
            End If
        Next i
    End With
End Sub
1
  • 2
    Side note: Indentation helps to see nesting. Adding indentation would be a good idea. Commented Jul 22, 2021 at 18:46

2 Answers 2

3
  1. Your variables i, j, x are not being assigned Data type, only y is being assigned as variant.

  2. If you are planning to use With construct then it should connect to its child objects via a . as demonstrated below.

  3. Your first two conditions have the same action associated so they can be joined by OR.

     Sub Calc()
    
     Dim ws As Worksheet
     Dim i, j, x, y
     Dim lrow As Long
    
     Set ws = Workbooks("BC.xlsm").Worksheets("AB")
     lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    
     With ws
         For i = 2 To lrow
             x = .Cells(i, 7).Value
             y = .Cells(i, 8).Value
             j = 9
             If y < 0 Or y <= x Then
                 .Cells(i, j) = y
             ElseIf y > x Then
                 Do Until y <= x
                     .Cells(i, j) = x
                     .Cells(i, j + 1) = y - x
                     y = y - x
                     j = j + 1
                 Loop
             End If
         Next i
     End With
    
     End Sub
    
Sign up to request clarification or add additional context in comments.

1 Comment

These changes works as I needed. Thank you so much.
1

I would use a for loop and some if logic inside:

Sub Calc()
    Dim ws As Worksheet
    Dim i As Long, j As Long, x as double, y as double
    Dim lrow As Long
    
    
    Set ws = Workbooks("BC.xlsm").Worksheets("AB")
    
    j = 9
    With ws
        lrow = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To lrow
            x = .Cells(i, 7).Value
            y = .Cells(i, 8).Value
            
            If y < 0 Then
                .Cells(i, j) = y
            ElseIf y <= x Then
                .Cells(i, j) = y
            ElseIf y > x Then
                For j = 9 To 8 + Application.RoundUp(y / x, 0)
                    If y >= x Then
                       .Cells(i, j) = x
                        y = y - x
                    Else
                        .Cells(i, j) = y
                    End If
                Next j
            End If
        Next i
    End With
End Sub

enter image description here

1 Comment

Thank you so much for the help. I tried this logic too but issue 3 seems to persist. I am not sure if I am doing something wrong or what but I copied and pasted the code as is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.