VBA to find value in a range and paste in adjacent cell

BMil8

Board Regular
Joined
Aug 9, 2010
Messages
153
Office Version
  1. 365
Hello, I have worksheet "Summary" with an account name in cell B4 (changes based on slicer selection). A user enters a comment in cell AC5. I'm looking for a macro that will take that comment and paste it into worksheet "Comments" in column D, after locating the account name from the Summary worksheet in a list that can be found in column B. Let me know if you need more info.

Thanks!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Put this in the Summary Sheet Module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$AC$5" Then
        Sheets("Comments").Columns(2).Find(Sheets("Summary").Cells(4, 2).Value, , , , 1).Offset(, 2).Value = Sheets("Summary").Cells(5, 29).Value
    End If
End Sub
 
Upvote 0
Try this

VBA Code:
Sub applyCommentToAccount()
    Set rng = Sheets("Comments").Range("B:B").Find(Sheets("Summary").Range("B4"), , xlValues, xlWhole)
    If Not rng Is Nothing Then
        rng.Offset(, 2).Value = Sheets("Summary").Range("AC5")
    Else
        MsgBox "No matching Account Name"
    End If
End Sub
 
Upvote 0
Solution
Or, if you want to run it from a button, in a regular module:
Code:
Sub Copy_Comment()
Dim shS As Worksheet, shC As Worksheet, acc As String, comm As String
Set shS = Worksheets("Summary")
Set shC = Worksheets("Comments")
acc = shS.Cells(4, 2).Value
comm = shS.Cells(5, 29).Value
    shC.Columns(2).Find(acc, , , , 1).Offset(, 2).Value = comm
End Sub
 
Upvote 0
Looks like I like commas. There should be just 3 commas, instead of 4, in the Find line.
 
Upvote 0
Looks like I like commas. There should be just 3 commas, instead of 4, in the Find line.
Thanks. I'm receiving an error "Run Time Error 91. Object Variable or With block variable not set." The "shC.Columns(2).Find(acc, , , 1).Offset(, 2).Value = comm". Tried researching and it says the object isn't Set. I'm not sure what that means though.
 
Upvote 0
Try this

VBA Code:
Sub applyCommentToAccount()
    Set rng = Sheets("Comments").Range("B:B").Find(Sheets("Summary").Range("B4"), , xlValues, xlWhole)
    If Not rng Is Nothing Then
        rng.Offset(, 2).Value = Sheets("Summary").Range("AC5")
    Else
        MsgBox "No matching Account Name"
    End If
End Sub
This gets the job done. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,729
Members
449,049
Latest member
MiguekHeka

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