Copy and paste as a comment from one Excel file to other Excel file

BBCC0000

New Member
Joined
Nov 2, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I'm not sure Excel VBA can do this, so I'm posting it to ask. Hope you guys can spend a little time to help me on this 😁

First step:
I have this Excel file (File A) which contains Number, Code and Your Name, and the name in cell E3. The name could be changed periodically (variable).

1702882494044.png


Second step:
I have another Excel file (File B) which contains many rows.

1702882654488.png


Third step:
I want file A copy their Code and paste to the matched Number of file B respectively as a COMMENT.
The name of the comment is from the file B cell E2.

1702882837863.png


I really appreciate if there is anyone who knows about this. Thanks for your help in advance!
 
You have followed me down to post 8 with various different amendments to the code, but now you are quoting post 3. Were you not getting errors before or had you not tested any of the code?

What line do you get the error on?
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You have followed me down to post 8 with various different amendments to the code, but now you are quoting post 3. Were you not getting errors before or had you not tested any of the code?

What line do you get the error on?

Because I'm thinking the post 3 macro first, after that only use post 8 macro...

Not sure which line, but it only shows Subscript Out of Range".
 
Upvote 0
The code in post 9 adds the comments and then reorders them in the same macro, have you tried that code?

If you get an error click the debug button and it should highlight the error line in yellow. You can then post the yellow line of code here.
 
Upvote 0
The code in post 9 adds the comments and then reorders them in the same macro, have you tried that code?

If you get an error click the debug button and it should highlight the error line in yellow. You can then post the yellow line of code here.

I have also tried post 9 code, it shows the same error but without highlighting the line in yellow....


1703121474766.png
 
Upvote 0
Open up the code window, put your cursor somewhere in the code you want to test, press F8, the code should run one line at a time each time you press F8. When you get the error, that will be the line that is causing the problem.

As a guess maybe the below lines:
Maybe the workbook name or sheet names are not right?
Are both of the workbooks below open when you run the code?
Is the code being run from one of the below workbooks?

VBA Code:
    Set ws1 = Workbooks("Book1.xlsx").Sheets("Sheet1")
    Set ws2 = Workbooks("Book2.xlsx").Sheets("Sheet1")
 
Upvote 0
Open up the code window, put your cursor somewhere in the code you want to test, press F8, the code should run one line at a time each time you press F8. When you get the error, that will be the line that is causing the problem.

As a guess maybe the below lines:
Maybe the workbook name or sheet names are not right?
Are both of the workbooks below open when you run the code?
Is the code being run from one of the below workbooks?

VBA Code:
    Set ws1 = Workbooks("Book1.xlsx").Sheets("Sheet1")
    Set ws2 = Workbooks("Book2.xlsx").Sheets("Sheet1")

Oh the yellow one stopped at ws1, because that file is a macro-enabled worksheet, so I put .xlsm
Does it matter? I tried for .xlsx too but it doesn't work and shows the same error

"Is the code being run from one of the below workbooks?" - I'm doing this, so that's the reason it wouldn't work?
 
Upvote 0
So does it work now or not?

Are both of the workbooks open when you run the code?
 
Upvote 0
And in the below, you are changing all of the orange parts to suit your situation, for example: you are not leaving it as 'Book1.xlsm' if you workbook is named 'MyWorkbookA' for example?
Also, are you updating the sheet names from 'Sheet1' to the names of the sheets you are using?

I just created two files with the code in 'File B' and run the below and it worked fine:
VBA Code:
Sub test()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng As Range, rCell As Range, str As String, nm As String
    Dim cTop As Long, cLeft As Long
   
    Set ws1 = Workbooks("File A.xlsx").Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet1")
   
    Set rng = ws2.Range("A2:A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row)
    nm = ws1.Range("E3")

    For Each rCell In rng
        If Application.CountIf(ws1.Range("A:A"), rCell) > 0 Then
            str = Application.VLookup(rCell, ws1.Range("A:B"), 2, 0)
            With rCell.Offset(, 3)
                If Not .Comment Is Nothing Then .Comment.Delete
                .AddComment nm & vbNewLine & str
                With .Comment.Shape.TextFrame
                    .Characters(1, Len(nm)).Font.Bold = True
                    .AutoSize = True
                End With
            End With
        End If
    Next rCell
   
    Set rng = rng.Resize(, 5)
    cTop = rng.SpecialCells(xlCellTypeComments)(1).Comment.Shape.Top
    cLeft = ws2.Range("G1").Left
    For Each rCell In rng.Cells
        With rCell
            If Not .Comment Is Nothing Then
                .Comment.Visible = True
                .Comment.Shape.Top = cTop
                .Comment.Shape.Left = cLeft
                cTop = .Comment.Shape.Top + .Comment.Shape.Height
            End If
        End With
    Next rCell
End Sub

The reason I changed the below:
VBA Code:
Set ws2 = ThisWorkbook.Sheets("Sheet1")

Is because of the the question i asked a few posts ago:
Is the code being run from one of the below workbooks?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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