Run VBA code when double clicking on range of cells

neuro1

New Member
Joined
Sep 9, 2022
Messages
8
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
Hi, I am very new to VBA in Excel and need some help figuring out how to accomplish this. So I have data in A2 to A100 and I want to run the below code when I double-click on the each cell in that range. It should run the same VBA code.

The code below the double click event basically takes the contents of A2 and copies it into a text file on my C:\ drive. I want to update this text file every time I click on a different cell.


VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
    
    Dim oShell As Object
    Set oShell = CreateObject("WSCript.Shell")
    Dim myFolder As String
    
    Application.DisplayAlerts = False
    
    ActiveSheet.Activate
    Range("A2").Select
    Selection.Copy
    
    'This temporarily adds a sheet named "Test."
    Sheets.Add.Name = "Test"
    Sheets("Test").Select
    ActiveSheet.Paste

    'Save selected data as text file in users selected folder.
    ActiveWorkbook.SaveAs Filename:="C:\work\imagepo.txt", FileFormat:=xlText, CreateBackup:=False
    
    'Remove temporary sheet.
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    'Indicate save action.
    MsgBox "Text File: " & myFolder & "Saved!"
    
    'Go to top of sheet.
    Range("A2").Select
    
    Application.DisplayAlerts = False
    
   Application.Quit
    
    End If
End Sub

Any help would be great!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Your code seems to do just what your said you want it to do. Not clear on what you need to change.

Are you saying that you want to use whatever cell was double-clicked as the update to the text file, rather than always using A2? If so:

Change
VBA Code:
    Range("A2").Select
    Selection.Copy
To
VBA Code:
    Target.Copy
Also you do not appear to be using oShell anywhere.
 
Upvote 0
Your code seems to do just what your said you want it to do. Not clear on what you need to change.

Are you saying that you want to use whatever cell was double-clicked as the update to the text file, rather than always using A2? If so:

Change
VBA Code:
    Range("A2").Select
    Selection.Copy
To
VBA Code:
    Target.Copy
Also you do not appear to be using oShell anywhere.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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