Results 1 to 3 of 3

Thread: Search/Replace in Comments in Excel

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52

    Search/Replace in Comments in Excel

    Hi All,
    I have been trying to write a macro which will allow me to replace all occurences of "Business" to "XXX" in the comments of all cells in an Excel Worksheet.

    please can someone help me in the process.
    thanks,
    Lonely
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

  2. #2
    New Member
    Join Date
    Jun 2003
    Location
    Dist 2, Ho Chi Minh City,Viet Nam
    Posts
    13
    In fact, I'm not very good at VBA, I only make some small macro for some excel file. But let's try this :

    Sub Comment()
    Range(Cells(1, 1), Cells(1000, 100)).Select
    Selection.ClearComments
    For i = 1 To 1000
    j = 1
    Do
    c = InStr(1, Trim(Cells(i, j)), "Business", 0) ' Choose 1 if you don't care for exact
    If c > 0 Then
    Cells(i, j).Select
    Selection.AddComment
    Selection.Comment.Text Text:="XXXX"
    End If
    j = j + 1
    Loop Until j = 100
    Next
    End Sub

    You can see my excel sample attched.

    Cheers
    Attached Files Attached Files

  3. #3

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52
    Hi Chien,
    Thank you very much for taking time off to help me out. However, with the help of some of my friends, i was able to come up with something like:

    VB Code:
    1. Sub comments_replace()
    2.  
    3. Dim FoundCell As Range
    4. Dim FindWhat As String
    5. Dim WithWhat As String
    6.  
    7. FindWhat = "User"
    8. WithWhat = "XXX"
    9.  
    10. Do
    11. Set FoundCell = ActiveSheet.Cells.Find(What:=FindWhat, _
    12. After:=ActiveCell, _
    13. LookIn:=xlComments, LookAt:=xlPart, SearchOrder:=xlByRows, _
    14. SearchDirection:=xlNext, MatchCase:=True)
    15.  
    16. If FoundCell Is Nothing Then
    17. Exit Do
    18. Else
    19. FoundCell.Comment.Text _
    20. Application.Substitute(FoundCell.Comment.Text, _
    21. FindWhat, WithWhat)
    22. End If
    23. Loop
    24.  
    25. End Sub

    Thought you might be interested in knowing

    Thanks,
    Lonely
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width