Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. Sub ExtractComments()
  2. Dim ExComment As Comment
  3. Dim i As Integer
  4. Dim ws As Worksheet
  5. Dim CS As Worksheet
  6. Set CS = ActiveSheet
  7. If ActiveSheet.Comments.Count = 0 Then Exit Sub
  8.  
  9. For Each ws In Worksheets
  10. If ws.Name = "Comments" Then i = 1
  11. Next ws
  12.  
  13. If i = 0 Then
  14. Set ws = Worksheets.Add(After:=ActiveSheet)
  15. ws.Name = "Comments"
  16. Else: Set ws = Worksheets("Comments")
  17. End If
  18.  
  19. For Each ExComment In CS.Comments
  20. ws.Range("A1").Value = "Comment In"
  21. ws.Range("B1").Value = "Comment By"
  22. ws.Range("C1").Value = "Comment"
  23. With ws.Range("A1:C1")
  24. .Font.Bold = True
  25. .Interior.Color = RGB(189, 215, 238)
  26. .Columns.ColumnWidth = 20
  27. End With
  28. If ws.Range("A2") = "" Then
  29. ws.Range("A2").Value = ExComment.Parent.Address
  30. ws.Range("B2").Value = Left(ExComment.Text, InStr(1, ExComment.Text, ":") - 1)
  31. ws.Range("C2").Value = Right(ExComment.Text, Len(ExComment.Text) - InStr(1, ExComment.Text, ":"))
  32. Else
  33. ws.Range("A1").End(xlDown).Offset(1, 0) = ExComment.Parent.Address
  34. ws.Range("B1").End(xlDown).Offset(1, 0) = Left(ExComment.Text, InStr(1, ExComment.Text, ":") - 1)
  35. ws.Range("C1").End(xlDown).Offset(1, 0) = Right(ExComment.Text, Len(ExComment.Text) - InStr(1, ExComment.Text, ":"))
  36. End If
  37. Next ExComment
  38. End Sub
  39.  
  40. ExComment.Parent.Address
  41.  
  42. Cells(1, ExComment.Parent.Column).Value
  43.  
  44. ws.Range("A2").Value = ExComment.Parent.End(xlUp).Value
  45.  
  46. If ws.Range("A2") = "" Then
  47. ws.Range("A2").Value = ExComment.Parent.End(xlUp).Value
  48. ws.Range("B2").Value = Left(ExComment.Text, InStr(1, ExComment.Text, ":") - 1)
  49. ws.Range("C2").Value = Right(ExComment.Text, Len(ExComment.Text) - InStr(1, ExComment.Text, ":"))
  50. Else
  51. ws.Range("A1").End(xlDown).Offset(1, 0) = ExComment.Parent.End(xlUp).Value
  52. ws.Range("B1").End(xlDown).Offset(1, 0) = Left(ExComment.Text, InStr(1, ExComment.Text, ":") - 1)
  53. ws.Range("C1").End(xlDown).Offset(1, 0) = Right(ExComment.Text, Len(ExComment.Text) - InStr(1, ExComment.Text, ":"))
  54. End If
  55.  
  56. ...
  57. Else: Set ws = Worksheets("Comments")
  58. End If
  59.  
  60. Dim iRow As Long ' you have a better control this way directly specifying the target cell
  61.  
  62. ' header needs to written only once - out of loop
  63.  
  64. ws.Range("A1").Value = "Comment In"
  65. ws.Range("B1").Value = "Comment By"
  66. ws.Range("C1").Value = "Comment"
  67.  
  68. With ws.Range("A1:C1")
  69. .Font.Bold = True
  70. .Interior.Color = RGB(189, 215, 238)
  71. .Columns.ColumnWidth = 20
  72. End With
  73.  
  74. iRow = 2 ' first empty row
  75. For Each ExComment In CS.Comments
  76. ws.Cells(iRow, 1).Value = CS.Cells(1, ExComment.Parent.Column) ' value in 1st row of column of comment
  77. ws.Cells(iRow, 2).Value = Left(ExComment.Text, InStr(1, ExComment.Text, ":") - 1)
  78. ws.Cells(iRow, 3).Value = Right(ExComment.Text, Len(ExComment.Text) - InStr(1, ExComment.Text, ":"))
  79. iRow = iRow + 1
  80. Next ExComment
  81.  
  82. End Sub
  83.  
  84. Sub ExtractComments()
  85.  
  86. Dim ExComment As Comment
  87. Dim i As Long
  88. Dim ws As Worksheet
  89. Dim CS As Worksheet
  90. Set CS = ActiveSheet
  91. If ActiveSheet.Comments.Count = 0 Then Exit Sub
  92.  
  93. For Each ws In Worksheets
  94. If ws.Name = "Comments" Then i = 1
  95. Next ws
  96.  
  97. If i = 0 Then
  98. Set ws = Worksheets.Add(After:=ActiveSheet)
  99. ws.Name = "Comments"
  100. Else: Set ws = Worksheets("Comments")
  101. End If
  102.  
  103. With ws
  104. .Range("A1").Value = "Comment In"
  105. .Range("B1").Value = "Comment By"
  106. .Range("C1").Value = "Comment"
  107. With .Range("A1:C1")
  108. .Font.Bold = True
  109. .Interior.Color = RGB(189, 215, 238)
  110. .Columns.ColumnWidth = 20
  111. End With
  112. For Each ExComment In CS.Comments
  113. .Range("A" & Rows.Count).End(xlUp)(2).Value = CS.Cells(1, ExComment.Parent.Column)
  114. .Range("B" & Rows.Count).End(xlUp)(2).Value = Left(ExComment.Text, InStr(1, ExComment.Text, ":") - 1)
  115. .Range("C" & Rows.Count).End(xlUp)(2).Value = Right(ExComment.Text, Len(ExComment.Text) - InStr(1, ExComment.Text, ":"))
  116. Next ExComment
  117. End With
  118.  
  119. End Sub
Add Comment
Please, Sign In to add comment