Populate cell values by clicking

Vikkii

New Member
Joined
Aug 5, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi, I want to populate a cell when I click another cell
Example: I have a list of data in Column A,
if I click any of the cells in Column A the value in that cell should populate to B1 or some cell which we want
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
something like this?

Book1
AB
1123123
2456
3789
4ABC
5DEF
6GHI
7
Sheet1


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Column = 1 Then
    Range("B1") = Target.Value  'Change "B1" for to the cell you want to receive the value
  End If
End Sub
 
Upvote 0
Solution
The Worksheet_SelectionChange event fires when clicking a cell but unfortunately, it also fires when selecting cells with the keyboard navigation keys. This is not acceptable. Fortunately, checking the state of the keys beforehand with a little call to the GetAsyncKeyState api function solves this problem.


Code in the worksheet module:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
#Else
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
#End If

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim i, nrow As Integer
    Dim vKeysArray As Variant, vKey As Variant
 
    vKeysArray = Array( _
        vbKeyReturn, _
        vbKeyTab, _
        vbKeyDown, _
        vbKeyUp, _
        vbKeyLeft, _
        vbKeyRight, _
        vbKeyHome, _
        vbKeyPageUp, _
        vbKeyPageDown, _
        vbKeyEnd _
    )

    For Each vKey In vKeysArray
        If GetAsyncKeyState(vKey) Then Exit Sub
    Next vKey
    If Target.Column = 1& Then
        Target.Offset(, 1&) = Target
    End If
    
End Sub
 
Upvote 0
something like this?

Book1
AB
1123123
2456
3789
4ABC
5DEF
6GHI
7
Sheet1


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Column = 1 Then
    Range("B1") = Target.Value  'Change "B1" for to the cell you want to receive the value
  End If
End Sub
Hi, I want to populate a cell when I click another cell
Example: I have a list of data in Column A,
if I click any of the cells in Column A the value in that cell should populate to B1 or some cell which we want
Could a Data Validation List in your target cell (the cell that is updated) be a solution?
 
Upvote 0
something like this?

Book1
AB
1123123
2456
3789
4ABC
5DEF
6GHI
7
Sheet1


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Column = 1 Then
    Range("B1") = Target.Value  'Change "B1" for to the cell you want to receive the value
  End If
End Sub
Example:

1691388676578.png

Here I did click "David" and you can see David in D2. Likewise I want to populate
If we click some cell, it should populate into the target cell (d2)
 
Upvote 0
You said:
if I click any of the cells in Column A the value in that cell should populate to B1 or some cell which we want

And how do you plan to tell the script the cell which you want?
 
Upvote 0
You said:
if I click any of the cells in Column A the value in that cell should populate to B1 or some cell which we want

And how do you plan to tell the script the cell which you want?

Sorry for the confusion, please take it as I want to populate to B1
1691391983717.png



Here I did click "David" and you can see David in D2. Likewise, I want to populate
If we click some cell, it should populate into the target cell (d2)
 
Upvote 0
It still seems confusing to me. :huh:
... please take it as I want to populate to B1

Here I did click "David" and you can see David in D2. Likewise, I want to populate
If we click some cell, it should populate into the target cell (d2)
 
Upvote 0
So if you click on "Alpha" which is in A1
You want Alpha entered into B1
Is that correct?
 
Upvote 0
I agree with The Worksheet_SelectionChange event fires when clicking a cell but unfortunately, it also fires when selecting cells with the keyboard.

So, I suggest using double click:
Double click any cell in column "A" and the script runs use a script like this:
This way you choose the range to enter the value in:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 1 Then
On Error GoTo M
Dim ans As String
ans = InputBox("Enter range to post value to", "Enter Range", "G5")
Range(ans).Value = Target.Value
End If
Exit Sub
M:
MsgBox "You Entered a Improper range" & vbNewLine & "You entered " & ans
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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