Clicking on a cell in Range to fill Another single cell Cell VBA

AoS80

New Member
Joined
Jan 31, 2023
Messages
6
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Mobile
Hello,

I am new to VBA and I am trying to type a code that allows me when I click on a cell within range for example A1:A20 it will copy that cell to C1

Also, lets say i want to do this multiple times in the same sheet. For example I also want to click on a cell within another range for example H1:H40 and it will copy that cell to D1

Please note that I am using Excel 2016

Appreciate your support in advance.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

You could test
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A1:A20")) Is Nothing Then Exit Sub
Range("C1").Value = Target.Value
Cancel = True
End Sub
 
Upvote 0
For your two ranges
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A1:A12,H1:H40")) Is Nothing Then Exit Sub
    If Target.Column = 1 Then
        Range("C1").Value = Target.Value
    Else
        Range("D1").Value = Target.Value
    End If
Cancel = True
End Sub
 
Upvote 0
Both are working perfectly! that's great James. Thank you for your support.
 
Upvote 0
You are welcome ;)
Just an update. I used the below code and the end result was when i double click anything from AA4:AA42 it copies the value to G2. However, when i click anything in the second range AB4:AB42 it replaces G2 instead of copying to G4. I tried to work around it but without any success. If you would take another look would really appreciate it.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("AA4:AA42,AB4:AB42")) Is Nothing Then Exit Sub
If Target.Column = 1 Then
Range("G4").Value = Target.Value
Else
Range("G2").Value = Target.Value
End If
Cancel = True
End Sub
 
Upvote 0
If my understanding is correct ...
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("AA4:AA42,AB4:AB42")) Is Nothing Then Exit Sub
If Target.Column = 27 Then
Range("G4").Value = Target.Value
Else
Range("G2").Value = Target.Value
End If
Cancel = True
End Sub
 
Upvote 0
If my understanding is correct ...
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("AA4:AA42,AB4:AB42")) Is Nothing Then Exit Sub
If Target.Column = 27 Then
Range("G4").Value = Target.Value
Else
Range("G2").Value = Target.Value
End If
Cancel = True
End Sub

You are Amazing! it so perfect doing exactly what i needed.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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