Delete specific text from cell comment?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
Office Version
  1. 365
Platform
  1. Windows
Hello there,

Is it possible to delete specific text from a cell comment with VBA? This is what I use to create a comment:

VBA Code:
    sText = ActiveCell.NoteText
    
    If ActiveCell.NoteText <> "" Then
    sText = sText & vbCrLf & "Lot-Specific Floor Plans"
    Else
    sText = sText & "Lot-Specific Floor Plans"
    End If
    
      With ActiveCell
        .ClearComments
        With .AddComment
            .Text sText
            With .Shape
                .TextFrame.Characters(1, InStr(sText, ":")).Font.Bold = False
                .Width = 180
                .Height = 60
                With .TextFrame.Characters.Font
                   .Name = "Tahoma"
                   .Size = 12
                End With
                With .TextFrame
                   .AutoSize = True
                End With
            End With
        End With
    End With

I need a script that will delete the line Lot-Specific Floor Plans from the comment, but keep all other text.

Possible?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe
sText = Replace(sText,"Lot-Specific Floor Plans","")
although why add it in the first place only to remove it later?
 
Upvote 0
Maybe
sText = Replace(sText,"Lot-Specific Floor Plans","")
although why add it in the first place only to remove it later?

I posted the wrong code. The IF line was me trying things. Your code works fine. The only issue is that it deletes the text correctly, but leaves an unsightly carriage return behind. So it looks like this:
 

Attachments

  • testy.png
    testy.png
    5.1 KB · Views: 17
Upvote 0
The leading carriage return is probably 2 characters long so maybe
sText = Replace(sText,"Lot-Specific Floor Plans","")
If Left(sText,2)=vbCrLf Or Left(sText2)=,2) = vbNewLine Then sText = Left(sText, Len(sText)-2)

That's off the top of my head. I would have to actually test it if it doesn't work for you.
EDIT- hang on, that would be if the CrLf was at the end (on the right)

change to If Left(sText,2)=vbCrLf Or Left(sText,2) = vbNewLine Then sText =Mid(sText,3)
note the correction to a Left function as well
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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