VBA to apply Function for all rows in Column

enriquer

New Member
Joined
Apr 28, 2020
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Hello! This is an example.
I have this table:

Total InvRequerido1Conf1Requerido2Conf2ValidacionTotal Conf
600​
550​
300​
600​
0​
600​
200​
250​
600​
0​
600​
150​
400​
600​
0​
600​
100​
500​
600​
0​
600​
250​
150​
600​
0​

What my code does is Substract check if Requerido1 is less or equal to Validacion. If its less it types the value of Requerido 1 in Conf1, then substracts validacion and adds to total Conf. Then does the same for Conf2 with what is remaining from Validacion:

Total InvRequerido1Conf1Requerido2Conf2ValidacionTotal Conf
600​
550​
550​
300​
50​
0​
600​
600​
200​
250​
600​
0​
600​
150​
400​
600​
0​
600​
100​
500​
600​
0​
600​
250​
150​
600​
0​

This is what i want it to do, but I want to apply it to every row in that column, not just row 2.

VBA Code:
Sub Prueba()

If Range("B2") <= Range("F2") Then
Range("C2").Value = Range("B2")
Else

If Range("B2") > Range("F2") Then
Range("C2").Value = Range("F2")
End If
End If

If Range("D2") <= Range("F2") Then
Range("E2").Value = Range("D2")
Else
If Range("D2") > Range("F2") Then
Range("E2").Value = Range("F2")

End If
End If

This is what i want it to do, but i want to apply it to every row in that column, not just row 2.


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Here is one way:
VBA Code:
Sub Prueba()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows starting on row 2
    For r = 2 To lr
    
        If Cells(r, "B") <= Cells(r, "F") Then
            Cells(r, "C").Value = Cells(r, "B")
        Else
            If Cells(r, "B") > Cells(r, "F") Then
                Cells(r, "C").Value = Cells(r, "F")
            End If
        End If

        If Cells(r, "D") <= Cells(r, "F") Then
            Cells(r, "E").Value = Cells(r, "D")
        Else
            If Cells(r, "D") > Cells(r, "F") Then
                Cells(r, "E").Value = Cells(r, "F")
            End If
        End If

    Next r

    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top