VBA FOR EACH + IF STATEMENT

smunk

New Member
Joined
Mar 19, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
hi guys! Hope you can help me here as I got stuck.

I have a column of phone numbers (over 140) and I want to create a VBA command allowing users to copy phone number by double clicking on the cell.
I know Javascript, but doesnt know VB very well.

I think my code should look something like:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

For Each c In Worksheets("Sheet1").Range("H13:H200").Cells

If Target.Address = c Then

Range(c).Copy

End If
Next

Cancel = True
End Sub



Please, tell me where am i wrong? Is my logic correct?
 
Indentation is not needed for the code to work properly but most people always use it because it makes it easy to follow the flow of the code especially when you use loops and "With .... End With" statements. I highly recommend using it.

You don't need a loop because you can double click only one cell at a time.
oh ok it makes sense. Can u pls tell me how can I add 2nd If statement for another column? this column contains email address. So same logic.

That doesnt work:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Target.CountLarge > 1 Then Exit Sub
If Intersect(Target, Range("L4:L200")) Is Nothing Then Exit Sub
Target.Copy

Else: If Target.CountLarge > 1 Then Exit Sub
If Intersect(Target, Range("K4:K200")) Is Nothing Then Exit Sub
Target.Copy

Cancel = True
End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("K:L")) Is Nothing Then Exit Sub
    Target.Copy
    Cancel = True
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("K:L")) Is Nothing Then Exit Sub
    Target.Copy
    Cancel = True
End Sub
works fine! thanks. yeah i get the logic. so we are just using 2 columns as one data set
 
Upvote 0
You are very welcome. :) K:L restricts the doubleclick event to any cell in those 2 columns.
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

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