Checking Threaded Comments - excel

My office just upgraded to a new version of Office 365, and with it came a new way that comments are done. The old comments are now referred to as "notes" and the new comments are now called "comments".
In one of my workbooks, I have a button that, when clicked, will look through certain cells to check whether there is a comment or not. It will then color the cell based on what it finds.
(my full code for the button is posted below)
Line 9 contains the issue
In previous versions of Excel, this button worked just fine. However, now it only works if the cells have "notes" and does not work if they have "comments". In my code, the class that I had been using was called "Comment", so my code was something along the lines of "If Cells(row, col).Comment Is Nothing...". This class still works, but only looks for notes. I have looked through the Object Library and under the hidden objects, I found a new class called "CommentThreaded". I tried changing my code to that ("If Cells(row, col).CommentThreaded Is Nothing...") but it does not work. When I click the button, I now get a run-time error: applictaion-defined or object-defined error when it tries to access this new class.
Does anyone know what I need to change to get my button to work with threaded comments?
Thanks,
Mike
Sub Comments()
Dim xrow As Integer
Dim xcol As Integer
For xrow = 7 To 88
For xcol = 3 To 15
If Cells(xrow, xcol).Value <= -0.1 Or Cells(xrow, xcol).Value >= 0.1 Then
If Cells(5, xcol).Value = "MTD %" Or Cells(5, xcol).Value = "YTD %" Then
If Not Cells(xrow, xcol).Comment Is Nothing Then
Cells(xrow, xcol).Interior.Color = RGB(155, 255, 188)
Else
Cells(xrow, xcol).Interior.Color = RGB(255, 255, 0)
End If
End If
End If
Next xcol
Next xrow
End Sub

As of May 15th 2019 the new object CommentThreaded is described by Microsoft.
In my Excel version 1906, it's fully supported in VBA.
Your assumed If Range.CommentThreaded Is Nothing works.
Here's some code to play with:
Private Sub ExcelsNewCommentThreaded()
Dim AllCommentsThreaded As Excel.CommentsThreaded
Dim OneCommentThreaded As Excel.CommentThreaded
Dim AllReplies As Excel.CommentsThreaded
Dim OneReply As Excel.CommentThreaded
Dim r As Range
Set AllCommentsThreaded = ActiveSheet.CommentsThreaded
' loop over all threaded comments of a worksheet and get their info
For Each OneCommentThreaded In AllCommentsThreaded
With OneCommentThreaded
Debug.Print .Author.Name, .Date, .Text
For Each OneReply In .Replies
With OneReply
Debug.Print .Author.Name, .Date, OneReply.Text
End With
Next OneReply
End With
Next OneCommentThreaded
Set r = Selection.Cells(1)
' check if the selected cell already contains a threaded comment
If r.CommentThreaded Is Nothing Then
r.AddCommentThreaded ("my new comment")
End If
With r.CommentThreaded
' get text of comment
Debug.Print .Text
' add some replies
.AddReply ("my reply 1")
.AddReply ("my reply 2")
' change text of comment
Debug.Print .Text(Text:="text of comment changed")
Debug.Print .Text
' change text of a reply
.Replies(1).Text Text:="text of reply 1 changed"
Debug.Print .Replies(1).Text
' delete second reply
.Replies(2).Delete
' delete whole comment including its replies
.Delete
End With
End Sub

Related

Trying to add yes/no vba message box into an existing working macro

I currently have a macro that works amazing well in Excel, but I'm wanting to add a Yes/No question into the VBA prior to starting the macro. The problem is I've tried several methods and looked for a way to make this work, and adding a simple one-line conditional statement isn't enough for what I'm trying to do.
Essentially, if the user selects Yes, I need the current macro to run in its entirety, but if they select No, I need a message box to popup stating they will need to complete the verification steps to continue. The current code (without any yes/no statement) is as follows... can anyone help?
Sub Submit_Details()
Dim shDrug As Worksheet
Dim shLogging As Worksheet
Dim iCurrentRow As Integer
Dim sDrugName As String
Set shLogging = ThisWorkbook.Sheets("Logging")
sDrugName = shLogging.Range("G11").Value
Set shDrug = ThisWorkbook.Sheets(sDrugName)
iCurrentRow = shDrug.Range("A" & Application.Rows.count).End(xlUp).Row + 1
With shDrug
.Cells(iCurrentRow, 1) = Format([now()], "DD-MMM-YYYY HH:MM:SS")
.Cells(iCurrentRow, 2) = shLogging.Range("G7")
.Cells(iCurrentRow, 3) = shLogging.Range("G8")
.Cells(iCurrentRow, 4) = shLogging.Range("G9")
.Cells(iCurrentRow, 5) = shLogging.Range("G10")
.Cells(iCurrentRow, 6) = shLogging.Range("G12")
.Cells(iCurrentRow, 7) = shLogging.Range("G13")
End With
shLogging.Range("G7, G8, G9, G10, G11, G12, G13").Value = ""
MsgBox "Data submitted successfully!"
End Sub
If you put this snippet first in the sub, it will get you started:
If MsgBox("Are you sure?", vbYesNo) <> vbYes Then
Exit Sub
End If

VBA listbox updates

i have below VBA code to update long list(1000) of part userform listbox with constant changes to design.
i need help with below 2 issues i am facing with code,
1)somehow, it is only updating only 1st selected item under multiselect listbox. can you pl help to check what is the issue with it to get all selected items updated by command button?
also, there are number of duplicates that i want to updates as well. however, below code updates only one and not other duplicate. can you pl help to correct code so it can update duplicates as well?
Private Sub cmdaction_Click()
Dim t, t1 As String
Dim vrech As Range, lColumn As Range
Dim sh As Worksheet
Dim i As Long
Dim selItem As String
Set sh = ThisWorkbook.Sheets("part bump")
Set lColumn = sh.Range("P1:AZA1").Find(Val(txtchangenumber.Value), , xlValues, xlWhole)
'Set lcolumn1 = sh.Range("F4:F1000")
If UserForm3.txtchangedescrption.Value = "" Then
MsgBox "Please enter Change Description"
Exit Sub
End If
If UserForm3.txtchangenumber.Value = "" Then
MsgBox "Please enter Change Number"
Exit Sub
End If
If UserForm3.cmbaction.Value = "" Then
MsgBox "Please Select part Action"
Exit Sub
End If
If lColumn Is Nothing Then
MsgBox "Change number not found"
Exit Sub
End If
With UserForm3.lstDatabase
For i = 0 To UserForm3.lstDatabase.ListCount - 1
If UserForm3.lstDatabase.Selected(i) = True Then
Set vrech = sh.Range("H4:H250").Find(.Column(7, i), , xlValues, xlWhole)
If Not vrech Is Nothing Then
Select Case cmbaction.Value
Case "RP"
t = Chr(Asc(Mid(.List(i, 7), 2, 1)) + 1)
t1 = Mid(.List(i, 7), 1, 2) & t & Mid(.List(i, 7), 4, 1)
Intersect(vrech.EntireRow, lColumn.EntireColumn) = t1
MsgBox "Selected parts 'RP' Action completed"
Case "RV"
Intersect(vrech.EntireRow, lColumn.EntireColumn) = .List(i, 7)
MsgBox "Selected parts 'RV' Action completed"
Case "DP"
Intersect(vrech.EntireRow, lColumn.EntireColumn) = "Deleted"
vrech.EntireRow.Font.Strikethrough = True
MsgBox "Selected parts 'DP' Action completed"
End Select
End If
End If
Next i
End With
End Sub
Upon further investigation I found that your handling of the Selected property is correct. I have deleted my advice in this regard and apologize for my hasty comment.
I have also re-examined your code and regret, I can't find a reason why it shouldn't deal with all selected items. without access to your workbook i don't have the ability to test and can't help you further.
Your second complaint is caused by this line of code.
Set vrech = sh.Range("H4:H250").Find(.Column(7, i), , xlValues, xlWhole)
It will find the first instance and no others. If you want the search to be repeated a loop will be required that repeats the search. Look up "VBA Find & FindNext MSDN" and you will find code samples how to construct the loop.
Note that in Dim t, t1 As String only t1 is a string. t is defined as a variant by virtue of not having a specified data type. This doesn't appear to be your intention.
I also noted your unusual use of Application.Intersect. Intersect(vrech.EntireRow, lColumn.EntireColumn) should be the equivalent of the simpler Sh.Cells(vrech.Row, lColumn), and it's recommended to specify the Value property when assigning a value to it.

Pass Arguments through Onaction with CheckBoxes

So i have been at this for a while now and I have searched through many websites and forums but alas I can not find a solution to my issue.
I am trying to add arguments to an .OnAction event for a Checkbox
So.. For example
Dim chk as Checkbox
With chk
.name = "chk" & .TopLeftCell.Offset(0, -7).Text
.Caption = ""
.Left = cel.Left + (cel.Width / 2 - chk.Width / 2) + 7
.Top = cel.Top + (cel.Height / 2 - chk.Height / 2)
.OnAction = "CheckboxHandle(chk)"
End With
So if I was trying to call this sub -> Public Sub CheckboxHandle(obj As CheckBox)
It requries a CheckBox Object to be able to run (this can change to a shape/Object if necessary)
THINGS I HAVE TRIED
Changing the data type to object and shape however i couldn't find a way to pass it through
Variations of the below statements
"""CheckboxHandle(chk)"""
"'CheckboxHandle" ""chk"" '"
Application.caller then looping through objects to find the object whit that name (this takes way too long as I have over 300 Checkboxes)
CONTEXT
In case the context helps I am trying to add a checkbox to every cell in a range and then have each one call the same method when they are clicked. I need the OnAction to send an Object as i look for the TopleftCell of the Object to change the colour of the adjacent cells
IN CASE IT IS HELPFUL
here is the method i would like to call from the OnAction Event
Public Sub CheckboxHandle(obj As CheckBox)
Dim rng As Range
'Sneaky sneaky changes
Application.ScreenUpdating = False
'For Loop to go through each of the cells to the left of the check box
For Each rng In Range(obj.TopLeftCell, obj.TopLeftCell.Offset(0, -7))
With rng
'if the checkbox is checked
If obj.Value = -1 Then
.Interior.Color = RGB(202, 226, 188)
'Adds the date and the person so you know who did the edit
obj.TopLeftCell.Offset(0, 1).Value = Now & " by " & Application.username
Else
'if it isn't checked
.Interior.Pattern = xlNone
'removes the edit name and date
obj.TopLeftCell.Offset(0, 1).Value = ""
End If
End With
Next rng
'Shows all the changes at the same time
Application.ScreenUpdating = True
'Changes the value of the progress bar to represent the project completion
If obj.Value = -1 Then
ActiveSheet.Range("E1").Value = ActiveSheet.Range("E1").Value + 1 / 207
Else
ActiveSheet.Range("E1").Value = ActiveSheet.Range("E1").Value - 1 / 207
End If
End Sub
Any help on this issue would be much appreciated
-Sebic0
I don't think that you can pass an object via the OnAction. The OnAction-property is a string holding the name of a Sub (plus parameter).
You could try to pass the name of the checkBox instead. Note that you have to enclose the name of the checkbox in double quotes, so that you would get something like. CheckboxHandle "chk123":
.OnAction = "'CheckboxHandle """ & .Name & """'"
And change your Action-routine
Public Sub CheckboxHandle(chbBoxName as string)
dim chk as CheckBox
Set chk = ActiveSheet.CheckBoxes(chkBoxName)
(...)

Runtime Error 13 - Mismatch

I'm new at VBA coding and working on a match code. The code is working just fine when I run the code in "Data sheet" (the sheet were all my data is and were the match has to be found), but when i'm run the code on the frontpage (Sheet 1 with userforms) the code is debuggen and says "Runtime Error 13". Can anybody tell what the problem is?
And can anybody tell me why my "If isError" doesn't work?
Thanks in advance!
Br
'Find SKU and Test number
Dim icol As Integer
Sheet13.Range("XFD2") = UserForm2.ComboBox1.Value 'Sættes = ComboBox1.value
Sheet13.Range("XFD3") = UserForm2.ComboBox2.Value 'Sættes = ComboBox2.value
icol = [Sheet13.MATCH(XFD2&XFD3,A:A&Q:Q,0)] 'Match af værdien for vores SKU og test nr
With ThisWorkbook.Worksheets("Data sheet")
'If SKU or Test number not found, then messagebox
If IsError("A:A") Then MsgBox "SKU not found": Exit Sub
If IsError("Q:Q") Then MsgBox "Test number not found": Exit Sub
'Add test result/next step and comment
.Cells(icol, 30).Value = Me.ComboBox3.Value
.Cells(icol, 30 + 1).Value = Me.Comments_To_Result.Value
End With
End If
Set objFSO = Nothing
Set openDialog = Nothing
Range("XFD2").Clear
Range("XFD3").Clear
icol should be like this:
icol = Application.match(arg1, arg2, arg3)
See the samples in MSDN:
var = Application.Match(Cells(iRow, 1).Value, Worksheets(iSheet).Columns(1), 0)
Concerning If IsError("A:A") Then MsgBox "SKU not found": Exit Sub, you are doing it wrongly. I assume, that you want to loop through all the cells in the first column and to get whether one of them is an error. You need a loop for this. This is a really simple one, but you should implement it somehow in your code:
Option Explicit
Public Sub TestMe()
Dim rng As Range
For Each rng In Range("A:A")
If IsError(rng) Then Debug.Print rng.Address
Next rng
End Sub

VBA search for string in document and check its color

I tried to make a function to search for a string in a document and check what is the first char in the string that is colored in red.
for example I know that my document contains the string "bread water juice peach wine". Imagine that the bold text is red colored. I want the function to return the int 19 (first red char - p).
Function check(stringToCheck As String) As Integer
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
With oRng.Find
' to ensure that unwanted formats aren't included as criteria
.ClearFormatting
'You don't care what the text is
.Text = stringToCheck
'Loop for each match and set a color
While .Execute
MsgBox (oRng.Text)
For i = 1 To 40
'take the Nth char of the string an check if it's red
'the following msgBox is working
MsgBox (Mid(oRng, i, 1))
If Mid(Orng, i, 1).Font.Color = wdColorRed Then
'the following msgBox is not working which means the error is in the last line.
MsgBox ("made it")
check = i
Exit Function
End If
Next i
Wend
End With
End Function
every time I try to call the function I have the error "run time error 424 - object required".
I added some msgboxes to see when is the function interrupted and added a comment in that place.
what is the problem? how can I fix it?
First thing's first: Use Option Explicit at the beginning of your module. You'll quickly find that your code has compilation issues.
Do you mean to use oRng or myRange? This should be consistent.
Once you've done that...
Mid(myRange, i, 1) returns a string, not an object.
You may want to use If oRng.Characters(1).Font.Color = wdColorRed Then instead.
Here's your code modified that returns correctly:
Function check(stringToCheck As String) As Integer
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
Dim i As Integer
With oRng.Find
' to ensure that unwanted formats aren't included as criteria
.ClearFormatting
'You don't care what the text is
.Text = stringToCheck
'Loop for each match and set a color
While .Execute
MsgBox (oRng.Text)
For i = 1 To 40
'take the Nth char of the string an check if it's red
'the following msgBox is working
MsgBox oRng.Characters(i)
If oRng.Characters(i).Font.Color = wdColorRed Then
'the following msgBox is not working which means the error is in the last line.
MsgBox ("made it")
check = i
Exit Function
End If
Next i
Wend
End With
End Function

Resources