0

I am using Excel 2010.

I have the following macro which is used to concatenate the string with cell values.

Sub Mac1()
Dim cell As Range
For Each cell In Range("D3", Range("D65536").End(xlUp))
    If cell.Value = "" Then
        cell.Value = ""
    Else
        cell.Value = cell.Value & " Day"
    End If
Next
End Sub

Note: Getting the string Day appended each and every time when i run the macro.

The expected result should be if the cell is empty then no string to be concatenate with the cell, if the cell is non empty then it should concatenate string Day at only one time with the cell value at the end.

1 Answer 1

3

Try is as a nested If to check if the string already ends in " Day".

Sub Mac1()
    Dim cell As Range

    With Worksheets("SHeet1")   'KNOW WHAT WORKSHEET YOU ARE ON!!!!!!
        For Each cell In .Range("D3", .Range("D65536").End(xlUp))
            If CBool(Len(cell.Value)) Then
                If Right(LCase(cell.Value), 4) <> " day" Then
                    cell.Value = cell.Value & " Day"
                End If
            End If
        Next cell
    End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Hey! Can you please help me out for this : stackoverflow.com/questions/38199207/…

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.