VBA: How to write a value to a specific cell on another worksheet?

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
301
Office Version
  1. 365
Using VBA, how do I take a value from Sheet 1 and write it to the cell I want on Sheet 2 when the row changes?

On Sheet 1, the active cell is E12.

EFGH
10melonBrianpantsgreen
11squashBarbarapantsblue
12grapeLoisblousered
13eggplantBruceshirtred

<tbody>
</tbody>

Cell B2 below (also on Sheet 1) is the value I want to write to a cell on Sheet 2 but for the row that "grape" is on .

AB
1
27

<tbody>
</tbody>


Sheet 2 looks like this:

ABCD
1appleweatherJohn0
2grapemountainLois7
3meloncarBrian2
3orangebikeSally2

<tbody>
</tbody>


The column I want to write the value in will always be D. I cannot figure out the code to find the row for "grape" (the value in the active cell on Sheet 1).
Sub CommandButton1_Click()​
Dim k As Long​
k = Range("B2").Select​
[mystery code to locate value of active cell and write k to the cell i want on sheet 2]
End Sub​



If I were using a formula, I'd use INDEX and MATCH. But not sure how to do it in VBA.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
rizzo93,

One approach would be to use the Find function...

Code:
Sub FindRow_1061388()
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet
Dim strng As String
Dim foundRow As Long

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
ws1.Activate
strng = ActiveCell.Value

On Error GoTo errHandler
foundRow = ws2.Cells.Find(What:=strng, After:=Cells(1, 1), LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
ws2.Cells(foundRow, 4).Value = ws1.Cells(2, 2)
ws2.Activate
errHandler:
    If Err.Number = 91 Then MsgBox "Search term not found."
End Sub
Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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