+ Reply to Thread
Results 1 to 7 of 7

Cuting Portion of Cell Value to Cell Comments

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    101

    Cuting Portion of Cell Value to Cell Comments

    I have an Excel 2007 workbook with several sheets, and various cells in several of the sheets have column headers with a description in the same cell enclosed in curly brackets, e.g. Header{Description}. I would like to to keep the column headers in their respective cells, but move (cut) the descriptions along with the brackets to the respective cell comments. Any ideas on how to do this in Excel VBA?

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: Cuting Portion of Cell Value to Cell Comments

    Hi
    try this Macro
    you will have to change set variable "rng" to the range you want to sort

    Sub Macro1()
    Dim rng As Range, CCell As Range, txt As String, ParseVal, CellText1 As String, CellText2 As String, CommentText As String
    Set rng = Range("A1:c2") 'range of cells to search
    For Each CCell In rng.Cells
        If Replace(CCell.Value, "{", "") <> CCell.Value And Replace(CCell.Value, "}", "") <> CCell.Value Then
            CellText1 = ""
            CellText2 = ""
            CommentText = ""
            ParseVal = Split(Replace(CCell.Value, "}", "{"), "{")
            CellText1 = Trim(Replace(ParseVal(0), "{", ""))
            CommentText = Trim(Replace(ParseVal(1), "{", ""))
            CellText2 = Trim(Replace(ParseVal(2), "{", ""))
            MsgBox CellText2
            If CellText2 <> "" Then CellText1 = CellText1 & ", " & CellText2
            CCell.Value = CellText1
            CCell.AddComment (CommentText)
        End If
    Next CCell
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    101

    Re: Cuting Portion of Cell Value to Cell Comments

    Thank you. This worked good for the active worksheet, what modifications would need to be made to work automatically for all worksheets in the workbook? Thanks again.

  4. #4
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: Cuting Portion of Cell Value to Cell Comments

    is it the same range in every sheet? if so, try:

    Sub Macro1()
    Dim rng As Range, CCell As Range, txt As String, ParseVal, CellText1 As String, CellText2 As String, CommentText As String, ws, ThisSheet as string
    
    Thissheet = activesheet.name
    
    for each ws in activeworkbook.worksheets
    
    Set rng = Range("A1:c2") 'range of cells to search
    For Each CCell In rng.Cells
        If Replace(CCell.Value, "{", "") <> CCell.Value And Replace(CCell.Value, "}", "") <> CCell.Value Then
            CellText1 = ""
            CellText2 = ""
            CommentText = ""
            ParseVal = Split(Replace(CCell.Value, "}", "{"), "{")
            CellText1 = Trim(Replace(ParseVal(0), "{", ""))
            CommentText = Trim(Replace(ParseVal(1), "{", ""))
            CellText2 = Trim(Replace(ParseVal(2), "{", ""))
            MsgBox CellText2
            If CellText2 <> "" Then CellText1 = CellText1 & ", " & CellText2
            CCell.Value = CellText1
            CCell.AddComment (CommentText)
        End If
    Next CCell
    next ws
    End Sub

  5. #5
    Forum Contributor
    Join Date
    06-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    101

    Re: Cuting Portion of Cell Value to Cell Comments

    Yes, it is for the same range, however, it still only works for the active sheet. After running it, I do get a pop-up Message Box with no message, which I click on "Ok" to dismiss.

  6. #6
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: Cuting Portion of Cell Value to Cell Comments

    sorry, I should always check code before posting!
    try this:

    Sub Macro1()
    Dim rng As Range, CCell As Range, txt As String, ParseVal, CellText1 As String, CellText2 As String, CommentText As String, ws, ThisSheet As String
    
    ThisSheet = ActiveSheet.Name
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.Select
    
      Set rng = Range("A1:c2") 'range of cells to search
      For Each CCell In rng.Cells
        If Replace(CCell.Value, "{", "") <> CCell.Value And Replace(CCell.Value, "}", "") <> CCell.Value Then
            CellText1 = ""
            CellText2 = ""
            CommentText = ""
            ParseVal = Split(Replace(CCell.Value, "}", "{"), "{")
            CellText1 = Trim(Replace(ParseVal(0), "{", ""))
            CommentText = Trim(Replace(ParseVal(1), "{", ""))
            CellText2 = Trim(Replace(ParseVal(2), "{", ""))
            MsgBox CellText2
            If CellText2 <> "" Then CellText1 = CellText1 & ", " & CellText2
            CCell.Value = CellText1
            CCell.AddComment (CommentText)
        End If
      Next CCell
    Next ws
    Sheets(ThisSheet).Select
    End Sub

  7. #7
    Forum Contributor
    Join Date
    06-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    101

    Re: Cuting Portion of Cell Value to Cell Comments

    Outstanding. Thanks for the quick assistance. It worked great.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1