I am using Excel Comments to store some information that I want to allow the user to edit using a form. I want to pre-populate the form with the text contained in the Comment when the user selects the Comment. I can do this when the user selects the cell with the red tab (when the Comments are hidden) using the SheetSelectionChange event. But is there a way to do this when the Comments are shown and the user clicks inside the Comment box?
I can't find any events associated with Comments in the help. Are there any Comment Events exposed to VBA? Or can I do this with some Cell or Sheet event? I tried putting a MsgBox inside the SheetSelectionChange Event to show the Target.ActiveCell, but when I select a Comment, I'm not getting a response, so it seems like when selecting a Comment, it is not associated with a sheet.
Thanks in advance for any tips.
I do not believe there are any events for comments.
However you could use the SelectionChange Event on the sheet:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (Target.Comment Is Nothing) Then MsgBox Target.Comment.Text
End Sub
To use within your Form:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (Target.Comment Is Nothing) Then
frmYourForm.Show
frmYourForm.txtComments = Target.Comment.Text
End If
End Sub
Related
I'm generating lines and changes and want to automate the linking with a macro
However i wish to add the link to the macro in a clickable cell.
I tried using the same way i would link a URL, to get started then i need to figure out if
i can assign the "SubAddress:=" to a macro instead of a link?
ws.Range("H6").Hyperlinks.Add anchor:=ws.Range("H6"), Address:="", SubAddress:="runMACRO", TextToDisplay:="Show tasks"
Update SOLVED --
the solution i went with was link my macro as "screentip" then following this to run it ;)
Sub Worksheet_FollowHyperlink(ByVal target As Hyperlink)
Application.Run target.ScreenTip
End Sub
Yes, you can, but not in that way... A trick must be used. Making a hyperlink for the cell itself and then using the WorksheetFollowHyperlink event for the Target.Parent.Address. Look here, please...
As stated in the comments, you can make use of the Workbook_SheetFollowHyperlink event handler. Hence, you would have to, in the ThisWorkbook module, to add something like the following code which would call runMACRO whenever a hyperlink in cell H6 is clicked.
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
If Target.Range.Address = "$H$6" Then
Call runMACRO
End If
End Sub
I'd like to know on how to code a userform (VBA Excel) with automatically updating itself when the cell values has changed.
I have produced a button that will show the userform with labels and text boxes. But whenever i click it yes it shows up but i need to click the userform in order for me to see the values.
Need help.
Thank you in advance,
Tramyer
The Worksheet.Change event is fired everytime you change the contents of a cell on a given worksheet.
You can create an event handler in the worksheet module (usually labeled Sheet1, Sheet2 etc. in the VBA editor) that is called every time the event is fired:
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
End Sub
This example just outputs the address of the cell that was changed, but you can adapt this to update your userform with the changed values instead.
I have been stuggling for same time now but I don't now how to do that...
When I select a cell in column Q, I get a userform (which is ok).
In that userform I have the content of the selected cell (e.g. Q6) (which is ok)
In userform I have two buttons:
- quit: which closes the userform without saving the text in the cell;
- save: which save the text in the cell from userform.
Both at saving and quitting the selections (focus) remain on that cell (Q6).
I want when I click again on the Q6 (witch is already selected), the userform reopen again.
The only solution I found so far, is to change the selection (for exemple on P6).
Cam anyone, please, help me? Thx.
In addition to how you are currently showing the form, you could add a double-click event handler. In the sheet's code module do something like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = Range("Q1").Column Then
UserForm1.Show
Cancel = True
End If
End Sub
Instruct the users that if they want to reshow the form, they should double click on the currently selected cell.
I am new to visual basic. I have a TextBox, and I want to trigger an event when the user lose focus on the textBox.
I tried writing
Private Sub TextBox_LostFocus()
something
End Sub
and
Private Sub TextBox_Leave()
something
End Sub
I don't really understand how they work, I have
Private Sub TextBox_Change()
something
End Sub
and that works fine, so what am I missing? How do I trigger the event when the user is not writing in the textbox anymore?
This is what you want:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
There's a helper -- when you're in the code editor, you'll see two list boxes above the editor. The list on the left contains the available objects for the current module. Select TextBox from there if it isn't already selected. The list box on the right contains the available Events. You should see Exit in there. Clicking that will paste in the code above.
How can I capture the event in Excel when a user clicks on a cell. I want to be able to use this event to trigger some code to count how many times the user clicks on several different cells in a column.
Check out the Worksheet_SelectionChange event. In that event you could use Intersect() with named ranges to figure out if a specific range were clicked.
Here's some code that might help you get started.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("SomeNamedRange")) Is Nothing Then
'Your counting code
End If
End Sub
Use the Worksheet.SelectionChange event to trap this.
Worksheet SelectionChange event would do it. Note that this fires every time user clicks a new cell.