Home » excel » Insert text next to selected cell – Excel VBA

Insert text next to selected cell – Excel VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an “If/Then” code snippet which changes the integer in a cell if the condition is true. What I would like to do is insert text next to that cell to indicate it has been manipulated.

Eg:

Public Sub testforfifty()
    Dim rcell As Range, rng As Range
    Set rng = Application.ActiveSheet.Range("D1:D" & Application.ActiveSheet.UsedRange.Rows.Count)
    For Each rcell In rng.Cells
        If rcell.Value > 50 And rcell.Value < 100 Then rcell.Value = rcell.Value - 50
        If rcell.Value >= 100 Then rcell.Value = "NoSplit"
    Next rcell
End Sub

If the cell is >=100 then the cell’s Value is just changed to "No Split" which is fine, but if the number is say 55, then the cell becomes the number 5. I want the cell next to it to say Manipulated now so the user can differentiate that the cell has been manipulated by the macro.

Can somebody point me in the correct direction? Thank You!

How to&Answers:

Split out your if statement from:

If rcell.Value > 50 And rcell.Value < 100 Then rcell.Value = rcell.Value - 50

To:

If rcell.Value > 50 And rcell.Value < 100 Then 
    rcell.Value = rcell.Value - 50
    rcell.offset(0,1).value = "Manipulated"
End if