简体   繁体   中英

Transfer values from one sheet of a workbook to another by clicking on the cell

I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?

To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet.D7.

If I recall correctly, the VBA code to do this would look something like this:

Private Sub WorkSheet_SelectionChange(ByVal Target As Range)

    'Check if it is in the range A1:A5000
    If Target.Column = 1 And Target.Row <= 5000 Then
        'get the target worksheet
        Dim TargetWS As Worksheet
        Set TargetWS = Sheets("OtherSheetsName")

        'copy the value of the just selected cell to D7 on the target WS
        TargetWS.Cells(7, 4).Value = Target.Value
    End If

End Sub

(Oops, forgot about the need for "SET" in VBA.)

You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000

Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box

Now you can select any of the 5000 part numbers from the dropdown in cell D7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM