If then VBA

Jaredbuoy

New Member
Joined
Oct 25, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am looking to recreate the following formular into VBA

=IF(I2=I1,N1&", "&E2,E2)

But it needs to loop through column "I" until there are no more to loop through. Pulling through the respective data of proceeding data. Essentially a drag fill function.

Can anyone kindly assist?

Many thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This function will write to column O. Change number "15" whichever column number you want to write your data.
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  For i = 2 To lRow
    If Cells(i, 9).Value = Cells(i-1, 9).Value Then
      Cells(i, 15).Value = Cells(i, 14).Value & ", " & Cells(i, 5).Value
    Else
      Cells(i, 15).Value = Cells(i, 5).Value
    End If
  Next
End Sub
 
Upvote 0
Hi
May be

VBA Code:
Sub test()
    With Range("A1:A" & Cells(Rows.Count, "I").End(xlUp).Row)
        .Formula = "=IF(I2=I1,N1&"",""&E2,E2)"
        .Value = .Value
    End With
End Sub
You may delete .value=.Value if you want to keep formula in
 
Upvote 0
Solution
Hi
May be

VBA Code:
Sub test()
    With Range("A1:A" & Cells(Rows.Count, "I").End(xlUp).Row)
        .Formula = "=IF(I2=I1,N1&"",""&E2,E2)"
        .Value = .Value
    End With
End Sub
You may delete .value=.Value if you want to keep formula in
Worked a charm thank you very much!
 
Upvote 0
You are very welcome
And thank you for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,993
Members
449,201
Latest member
Lunzwe73

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