Linking Comments

AutomationNation

New Member
Joined
Feb 21, 2012
Messages
6
Hello All,

I have a problem that has proven to be pretty challenging. I need to do a Paste Link and include the comments. The cell comments are lost when executing a Paste Link. I have two workbooks and I need to link several rows of data from one to the other. The comments contain valuable information and it is desirable to see those comments in both workbooks. Does anyone know how to do this using VBA? I've tried and failed. All help would be appreciated!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Thanks for your input. I'm currently doing what you suggested. Paste Link only brings over the value of the cell (no comments). I can't figure out how to link the comments as well as the values of the cells.
 
Upvote 0
Oh Nice! That's pretty cool but it's not quite what I'm looking for. The comments aren't dynamic. I need the comments to update along with the values of the cells if they are changed in the original workbook. Any other ideas?
 
Upvote 0
...The comments contain valuable information and it is desirable to see those comments in both workbooks. ...

Comments are really meant for metadata or explanation. If the contents of these comments are as valuable as you imply, it may make more sense to put them into cells too.

Do you also intend for comments to change as needed? That is, if the value in SheetX!Y99 in workbook A changes and its comment also changes, the link in worksheet B would update with the changed value from workbook A, but the comment wouldn't. Do you want changes in comments to propagate? If so, you'd need to use a macro to open workbook A to get the comments because comments are inaccessible from closed workbooks. You'd need to check cells containing formulas, check whether those formulas were just links into workbook A, if so get the cell referred to in the link, and copy its comment to the cell in workbook B. This would be a lot easier if these weren't cell comments but contents of other cells.
 
Upvote 0
The source workbook would need to be open because cell comments wouldn't be accessible otherwise (at least not much short of writing your own workbook parser in VBA). Changing comments doesn't trigger any event, so you'd need to use Application.OnTime to run a macro periodically to copy all comments.
 
Upvote 0
Yes, I do intend for the comments to change as needed. I would like the comments changes to propagate.

I agree that it would be much easier if the valuable info was in cells instead of comments. The reason I can't is because of space savings. I would have to add a column for every existing column just to put the comments in which would double the width of the workbook. I figured that I would need a macro but I'm don't have enough programming under my belt to be able to write one of this nature.
 
Upvote 0
This requires moderately nontrivial VBA. The approach I'd take would only propagate comments in cells already containing comments and containing simple single-cell link formulas into other workbooks. Determining that specific type of formula is too difficult to do with plain VBA (without getting paid), so I'm using regular expressions. You need to set a reference to the VBScript regular expressions DLL in your workbook's VBE project.

Once the macro determines that a cell contains a simple link formula, it opens closed workbooks and leaves them open while it iterates through all cells in the active worksheet's UsedRange, and closes them at the end.

This only operates on the active worksheet, and as a macro it'd only run when you call it. You need to add this code to a general VBA module: press [Alt]+[F11] to display the VB Editor, and issue the menu command Insert > Module. Left up to you to learn enough VBA to figure out how to use this on multiple worksheets and run it automatically periodically.

Note: this macro dies silently on invalid link formulas, i.e., links to nonexistent workbooks. Left as an exercise how to display missing file error messages and resume with next cell.

I still believe this is a bad design. I've left a fair amount of work for you to do to make it robust to give you an incentive to learn VBA. Who said free help was free?

Code:
Sub propagatecomments()
  'REQUIRES a reference to the Microsoft VBScript Regular Expressions 5.5 DLL
  Const CAPAT = "([^\\:!\[\]]+)!(\$?[A-Z]{1,3}\$?\d{1,7})"
  Const FNPAT = "\[([^\\:\[\]]+)\]"
  Const DDPAT = "((\\\\[^\\:\[\]]+\\)|([A-Z]:\\))([^\\:\[\]]+\\)*"

  Dim c As Range, rc As Range, re As New RegExp, mc As MatchCollection
  Dim cf As String, fn As String, fnc() As String, j As Long, p As Long

  On Error GoTo CleanUp
  With Application
    .EnableEvents = False
    .Calculation = xlCalculationManual
    .DisplayAlerts = False
    .EnableCancelKey = xlDisabled
    .ScreenUpdating = False
  End With

  ReDim fnc(1 To 16)

  re.Global = True
  re.IgnoreCase = True

  For Each c In ActiveSheet.UsedRange
    'cell must contain both formula and comment
    If Not c.HasFormula Then GoTo Continue
    If c.NoteText = "" Then GoTo Continue

    'remove initial = and single quotes (if any)
    cf = Replace(Mid$(c.Formula, 2), "'", "")
    fn = ""
    p = 1

    'check whether formula begins with drive/directory path
    re.Pattern = DDPAT
    Set mc = re.Execute(Mid$(cf, p))
    'multiple paths means not a simple link formula, so abort
    If mc.Count > 1 Then GoTo Continue
    If mc.Count = 1 Then
      With mc.Item(0)
        'anything between pointer and path, abort
        If .FirstIndex > 0 Then GoTo Continue
        'simple link so far, so store path and advance pointer
        fn = .Value
        p = p + .Length
      End With
    End If
    Set mc = Nothing

    'check whether next piece of formula is [filename]
    re.Pattern = FNPAT
    Set mc = re.Execute(Mid$(cf, p))
    'many bracketed text means not a simple link formula, so abort
    If mc.Count > 1 Then GoTo Continue
    If mc.Count = 1 Then
      With mc.Item(0)
        'anything between pointer and [filename], abort
        If .FirstIndex > 0 Then GoTo Continue
        'simple link so far, so append filename to nonblank path
        'and advance pointer
        If fn <> "" Then fn = fn & Mid$(.Value, 2, Len(.Value) - 2)
        p = p + .Length
      End With
    End If
    Set mc = Nothing

    'check whether next piece of formula is single cell address
    re.Pattern = CAPAT
    Set mc = re.Execute(Mid$(cf, p))
    'many cell addresses means not a simple link formula, so abort
    If mc.Count > 1 Then GoTo Continue
    If mc.Count = 1 Then
      With mc.Item(0)
        'anything between pointer and cell address, abort
        If .FirstIndex > 0 Then GoTo Continue
        'simple link so far, so advance pointer
        p = p + .Length
      End With
    End If
    Set mc = Nothing

    'anything at or after pointer, abort
    If Len(cf) >= p Then GoTo Continue

    'at this point the formula is a simple link
    'if fn is nonblank, it refers to a closed file which must be opened
    If fn <> "" Then
      j = j + 1
      'expand fnc as needed by doubling its size
      If j >= UBound(fnc, 1) Then ReDim Preserve fnc(1 To 2 * UBound(fnc, 1))
      fnc(j) = fn
      Workbooks.Open Filename:=fn, UpdateLinks:=0
    End If

    'get Range object reference to linked cell
    Set rc = Evaluate(Mid$(c.Formula, 2))
    If rc.NoteText = "" Then
      c.Comment.Delete
    Else
      c.NoteText Text:=rc.NoteText, Start:=1
    End If

Continue:
  Next c

  For j = j To 1 Step -1
    fn = fnc(j)
    Workbooks(Mid$(fn, InStrRev(fn, "\") + 1)).Close SaveChanges:=False
  Next j

CleanUp:
  With Application
    .ScreenUpdating = True
    .EnableCancelKey = xlInterrupt
    .DisplayAlerts = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
  End With

End Sub
 
Upvote 0
Good grief! Thank you so much hrlngrv!!! You are def. schooling me! I appreciate your help and efforts on this problem. I'm glad you left a little for me to figure out. It'll help me grow as a programmer. I'm going to go through this on my own and make sure I understand everything. I'll post again what I add or modify. Thanks again!!
 
Upvote 0

Forum statistics

Threads
1,215,833
Messages
6,127,161
Members
449,367
Latest member
w88mp

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