Hi I have an excel macro question in VBA. I have this code. I want it to go down each cell in row C and run the same if statement instead of just depending on cell C2. I want it to continue to C3, C4, C5, all the way till the end of the sheet. Can anyone help me solve this?

Sub Macro1()



Dim lngLastRow As Long
    lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    If IsEmpty(Range("C2")) Then
        Range("E2:E" & lngLastRow).Formula = "=B2 & ""_"" & C2"
    Else
    Range("E2:E" & lngLastRow).Formula = "=B2"
    End If
    End Sub

Recommended Answers

All 2 Replies

To have you on the path to a solution: Make a Do While loop with your found last row!
use a Counter as variable for creating your range in "Cn" and match where you are.
something like:
Dim MyRowCounter as Integer = 2
Do While MyRowCounter =< LngLastRow
concatenate your Range
do your If
MyRowCounter = MyRowCounter + 1
loop

btw. a long is big I think an integer is enough for a counter in Excel! (lngLastRow)
Succces, I hope my help gets you further on your way..
Rene

commented: Good advice +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.