VBA for open comment by clicking cell, formatting, paste and clear all from clipboard

vicwootn

New Member
Joined
Oct 2, 2013
Messages
21
I've been googling this for awhile. Sometimes I think I get pieces of the code but seem to ulimately fall short. Anything you can do to help would mean so much to me. Here is what I'm looking for.

When I have exactly four items in my Office Clipboard I want to Paste All into the comment of the selected cell then Clear All inside the clipboard. I would prefer never to have to open the comment myself.

I have some specific conditions for the paste. Before pasting, if there is already text inside the comment I want it all deleted then to be cursored at the top of the blank comment, then Paste All. If there is no existing comment I want to insert one, delete the comment heading, then cursor me at the top of the blank comment, then Paste All.

The Paste All needs to be bold text.

Each item is a decimal number to the tenth or hundredth. The last item will always be exactly two numbers to the tenth or hundredth. Here is an example of what the clipboard will look like :

Click an item to paste :
7.24
7.7
9.65
8.4 8.21

When I Paste All, I would like to swap the last two numbers and add a Return space between them, formatted like :

7.24
7.7
9.65
8.21

8.4

Again, any help would be deeply appreciated even if it's pieces.
Thank you!!!
 
I have some specific conditions for the paste. Before pasting, if there is already text inside the comment I want it all deleted then to be cursored at the top of the blank comment, then Paste All. If there is no existing comment I want to insert one, delete the comment heading, then cursor me at the top of the blank comment, then Paste All.

Accessing the comment object through VBA in excel does not operate in such a way where you would be required to move your cursor or remove any heading. I believe that heading you are referring to is only created if you insert a comment through excel's menus.

Code:
Sub commentHandler()


    Dim rng As Range
    Dim myData As String
    
    Set rng = Range("A1")
    
    myData = "lorem ipsum"


    'if no comment exists
    If rng.Comment Is Nothing Then
    
        'create one
        rng.AddComment
        
        'and insert your data
        rng.Comment.Text myData
        
    'if a comment exists and contains data
    ElseIf rng.Comment.Text <> "" Then
    
        'clear it
        rng.clearComments
        
    End If


End Sub
 
Last edited:
Upvote 0
Upvote 0
Virtually everything you need to build the foundational code are in those links. Formatting your data is easy and should worry about that after you've fleshed out the basic functions.

Code:
'Clearing the Office Clipboard


Dim oData   As New DataObject 'object to use the clipboard


oData.SetText text:=Empty 'Clear
oData.PutInClipboard 'take in the clipboard to empty it
 
Upvote 0
I've been digging into this some more. From what i've been reading, the office clipboard is not exposed to access by code. The only exception has been Office 2000 where you were able to access it in a limited manner through the commbar bar that used to be present. Now days, you can only use VBA to access the Windows Clipboard.

Alternatively, use a macro to check the clipboard every second and copy any new entries into a worksheet. Then manipulate the data from that worksheet as you need. This is likely your only option.

Here are some example workbooks to look at. Workbook Example #1 copies the last data from the clipboard and pastes it into a cell's comment. If no comment exists, it inserts one. If one already exists, it clears it and then pastes the clipboard data.

Workbook Example #2 will check the windows clipboard every second. If the last entry in the clipboard is different than the last item copied to the worksheet, it adds it.

Workbook Example #1: https://1drv.ms/x/s!AnpELGec0aCqlbo-qtnZOP-2uXWoaQ
Workbook Example #2: https://1drv.ms/x/s!AnpELGec0aCqlbpS7tju4GvgRmTGNQ
 
Upvote 0

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