need help with instr() to find a section of comment text to replace

sumhungl0

Board Regular
Joined
Jan 1, 2014
Messages
119
i need to find a way of editing a section of comment text. i have most of the code figured out already. my macro button takes the info contained in the first few columns of one row in the active sheet and compiles this info into a comment for a found cell in sheet1. sometimes the info in the active sheet will be updated and will need to replace the info of the comment in sheet1. my only problem is that i need to be able to replace a section of text in a comment with a vba macro, not the whole comment. the start of the text to be replaced will always be the text in column A of the active sheet. the end of the text to be replaced is a number but it can be one or two digits long and is from column G in the active sheet. this number can be anywhere from 1 to 50 ish and will always be preceded by Children. example: "Children 15" or "Children 4". i was thinking maybe using the word Children to find the numeric characters after, but i don't know how to do this. anybody have any ideas? thanks.

Code:
Sub setComment4Tour()
    On Error GoTo hell
    Dim wrow As Range
    Dim id, AC As String
    Dim SearchRange As Range
    Dim wcol As Range
    Dim fdate As Date
    Dim fcell As Range
    If Not Intersect(ActiveCell, Range("aa:aa")) Is Nothing Then
'       check for current sheet activecell value in other sheet range
        If Range("A" & ActiveCell.Row) <> "" And Range("C" & ActiveCell.Row) <> "" Then
'           check for values in current sheet col A & C
            id = ActiveCell.Value
            fdate = Range("C" & ActiveCell.Row).Value
'           Find row ref
            Set wrow = Worksheets("WEEKLY").Range("a4:a13").Find(id, lookat:=xlPart)
            If Not wrow Is Nothing Then
            End If
'           Find column ref
            Set SearchRange = Worksheets("WEEKLY").Range("3:3")
            Set wcol = SearchRange.Find(fdate, LookIn:=xlValues, lookat:=xlWhole)
            Set fcell = Worksheets("WEEKLY").Cells(wrow.Row, wcol.Column)
'           combine row and column to get target cell
            If Not InStr(UCase(fcell), "TOUR") <> 0 Then
                mb1 = MsgBox("The WEEKLY does not have a tour scheduled for " & id & "." & Chr(10) & "Would you like to create the info comment for " & id & " anyway?", vbYesNo, " Tour Not Found!")
                If mb1 = vbYes Then
                    GoTo updateComment
'                   Resume Next
                Else
                    GoTo hell
                End If
            End If
'           MsgBox "cell " & fcell.Address
updateComment:
'           new comment based on current sheet info in the activecell row
            newcmnt = Range("A" & ActiveCell.Row).Value & Chr(10) & Range("D" & ActiveCell.Row).Value & "-" & Range("E" & ActiveCell.Row).Value & Chr(10) & "Adults " & Range("F" & ActiveCell.Row).Value & Chr(10) & "Children " & Range("G" & ActiveCell.Row).Value
            If fcell.Comment Is Nothing Then
'               Set ctext = Worksheets("WEEKLY").Cells(wrow.row, wcol.Column).Comment
'               fcell.Comment.Text Text:=atext
                fcell.AddComment Text:=newcmnt
                fcell.Comment.Shape.TextFrame.AutoSize = True
                MsgBox "comment added"
            ElseIf InStr(fcell.Comment.Text, Range("A" & ActiveCell.Row).Value) <> 0 Then
'               check if comment title already exists
                MsgBox "Tour " & Range("A" & ActiveCell.Row).Value & "'s info comment already exists on the WEEKLY."
            Else
'               ammend current comment with additional comment
                cmnt = fcell.Comment.Text
                newcmnt = cmnt & Chr(10) & Chr(10) & Range("A" & ActiveCell.Row).Value & Chr(10) & Range("D" & ActiveCell.Row).Value & "-" & Range("E" & ActiveCell.Row).Value & Chr(10) & "Adults " & Range("F" & ActiveCell.Row).Value & Chr(10) & "Children " & Range("G" & ActiveCell.Row).Value
                fcell.Comment.Text Text:=newcmnt
                fcell.Comment.Shape.TextFrame.AutoSize = True
                MsgBox "comment added"
            End If
        Else
            MsgBox "There is not a Tour or Date on this Row."
            GoTo hell
        End If
    Else
        MsgBox "Select the cell with the Aircraft that you would like to create a Comment for, and try again."
    End If
    Exit Sub
hell:
'   MsgBox "No Comment"
End Sub
 
Last edited by a moderator:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Could you provide an example; current comment text, cell values, expected result comment?
 
Upvote 0
Could you provide an example; current comment text, cell values, expected result comment?
example:
active sheet columns: A-group name, C-date, D-time start, E-time end, F-#adults, G-#children. sample row data: Cub Scout Pack 709, 3-Aug, 0900, 1100, 3, 13. sample comment: Cub Scout Pack 709 0900-1100 Adults 3 Children 13. the vba that creates the comment text from the row of active sheet is..
Code:
newcmnt = Range("A" & ActiveCell.Row).Value & Chr(10) & Range("D" & ActiveCell.Row).Value & "-" & Range("E" & ActiveCell.Row).Value & Chr(10) & "Adults " & Range("F" & ActiveCell.Row).Value & Chr(10) & "Children " & Range("G" & ActiveCell.Row).Value
 
Upvote 0
The part I don't understand is what is currently in the comment and how to determine what part of it you want replaced. It didn't sound like you wanted to replace it all. A before and after comment example would help.
 
Upvote 0
I'm not exactly sure what you are doing, but this will find the location and value of "Children nn" in a string
Code:
Dim someString As String, ChildrenStart As Long

someString = "abc def ghi Children 23 cats"

ChildrenStart = InStr(1, someString, "Children ")

MsgBox Val(Mid(someString, InStr(ChildrenStart, someString, " ")))
 
Upvote 0
The part I don't understand is what is currently in the comment and how to determine what part of it you want replaced. It didn't sound like you wanted to replace it all. A before and after comment example would help.
i need to replace the whole part of the comment from the group name(from col A) to the children nn part(from col G). the problem is that section of info can be anywhere in the comment, start, middle, end. that is why I need to be able to identify that section of the comment to replace it. the name of the group(from col A) will always stay the same. the other info for that section of the comment can change. so that is the reason I was looking to identify the number after the word Children(from col G), however that number can be one or two digits(n or nn). so the comment section might be like "Cub Scout Pack 709, 0900-1100, Adults 3, Children 13" and change to "Cub Scout Pack 709, 0800-1200, Adults 4, Children 16". the info is changed in the active sheet and I am trying to have my macro button run code to replace that section of the comment on sheet1. hope that helps and thanks for helping me.
 
Upvote 0
Try something like this...

Code:
                cmnt = fcell.Comment.Text
                lStart = InStr(1, cmnt, Range("A" & ActiveCell.Row).Value, 1)
                lLen = InStr(lStart, cmnt, "Children", 1) + 11 - lStart [COLOR=green]'assume two digits for now[/COLOR]
                strReplace = Mid(cmnt, lStart, lLen)
                [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] Right(strReplace, 1) [COLOR=darkblue]Like[/COLOR] "#" [COLOR=darkblue]Then[/COLOR] strReplace = Left(strReplace, Len(strReplace) - 1)    [COLOR=green]'remove last char if not the 2nd digit[/COLOR]
                newcmnt = Range("A" & ActiveCell.Row).Value & Chr(10) & Range("D" & ActiveCell.Row).Value & "-" & Range("E" & ActiveCell.Row).Value & Chr(10) & "Adults " & Range("F" & ActiveCell.Row).Value & Chr(10) & "Children " & Range("G" & ActiveCell.Row).Value
                cmnt = Replace(cmnt, strReplace, newcmnt)
                fcell.Comment.Text Text:=cmnt
                fcell.Comment.Shape.TextFrame.AutoSize = [COLOR=darkblue]True[/COLOR]
 
Upvote 0
Try something like this...

Code:
                cmnt = fcell.Comment.Text
                lStart = InStr(1, cmnt, Range("A" & ActiveCell.Row).Value, 1)
                lLen = InStr(lStart, cmnt, "Children", 1) + 11 - lStart [COLOR=green]'assume two digits for now[/COLOR]
                strReplace = Mid(cmnt, lStart, lLen)
                [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] Right(strReplace, 1) [COLOR=darkblue]Like[/COLOR] "#" [COLOR=darkblue]Then[/COLOR] strReplace = Left(strReplace, Len(strReplace) - 1)    [COLOR=green]'remove last char if not the 2nd digit[/COLOR]
                newcmnt = Range("A" & ActiveCell.Row).Value & Chr(10) & Range("D" & ActiveCell.Row).Value & "-" & Range("E" & ActiveCell.Row).Value & Chr(10) & "Adults " & Range("F" & ActiveCell.Row).Value & Chr(10) & "Children " & Range("G" & ActiveCell.Row).Value
                cmnt = Replace(cmnt, strReplace, newcmnt)
                fcell.Comment.Text Text:=cmnt
                fcell.Comment.Shape.TextFrame.AutoSize = [COLOR=darkblue]True[/COLOR]
PERFECT! that worked great. thank you so much. ive tested it out several different scenarios and it works perfect for what I need to do. it finds the section of comment text and doesn't pick up any of the other text. thanks again, you have helped me out a great deal. now all I need to do is wrap this code into a msgbox vbyesno for the user to decide to replace or not. have a great day.
 
Upvote 0

Forum statistics

Threads
1,216,138
Messages
6,129,099
Members
449,486
Latest member
malcolmlyle

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