Consulting

Results 1 to 6 of 6

Thread: Kick off macro when a cell is double clicked?

  1. #1
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    5
    Location

    Kick off macro when a cell is double clicked?

    Does anyone have any tips for a macro that kicks off when a cell is double clicked?

    What if it's multiple cells that get double clicked?

    Thanks so much for help in advance! I really appreciate it!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use the BeforeDoubleClick event procedure.

    Test if Target.Cells.Count > 1.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    5
    Location
    Thanks for replying! I tried putting in at the beginning of my macro but it's not working. I highlighted below in red where I would normally double click. I just need the macro to automatically kick off any time I double click a cell and not just E8. Thanks again!

     Private Sub Worksheet_Change(ByVal Target As Range)
     
     If Target.Cells.Count > 1 Then
     
        Sheets("Test").Select
        Application.DisplayAlerts = False
        ActiveWindow.SelectedSheets.Delete
        Application.DisplayAlerts = False
        Sheets("Data").Select
        ActiveWindow.SelectedSheets.Delete
        Sheets("Sheet4").Select
        Range("E8").Select
        Application.DisplayAlerts = False
        Sheets(1).Name = "Sheet2"
        Sheets("Sheet2").Delete
        Application.DisplayAlerts = True
        Selection.ShowDetail = True
        ActiveSheet.Name = "Data"
        Sheets.Add.Name = "Test"
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Data!R1C1:R50000C11", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
            :="Test!R3C1", TableName:="PivotTable1", DefaultVersion:= _
            xlPivotTableVersion14
        Sheets("Test").Select
        Cells(3, 1).Select
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Payer")
            .Orientation = xlRowField
            .Position = 1
        End With
        ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
            "PivotTable1").PivotFields("new charge"), "Sum of new charge", xlSum
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of new charge")
            .Caption = "Count of new charge"
            .Function = xlCount
        End With
        Range("B7").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Payer").AutoSort _
            xlDescending, "Count of new charge", ActiveSheet.PivotTables("PivotTable1"). _
            PivotColumnAxis.PivotLines(1), 1
        Rows("4:4").Select
        ActiveWindow.FreezePanes = True
        Range("C2").Select
        
        
    End Sub
    Last edited by Bob Phillips; 09-17-2014 at 01:16 AM. Reason: Added VBA tags

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    IF you doubleclick, it is for one cell only or the cells are merged that you are editing.

    e.g.
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)  
     MsgBox "BeforeDoubleClick: " & Target.Address & vbLf & _
        "Selection Cell Count: " & Selection.Cells.Count
      
      If Target.Address(False, False) = "A5" Then
        MsgBox "A5 was doubleclicked."
        Cancel = True
      End If
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      MsgBox "SelectionChange: " & Target.Address & vbLf & _
        "Selection Cell Count: " & Selection.Cells.Count
    End Sub

  5. #5
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    5
    Location
    Would I use this at the beginning Kenneth?

    Thanks!

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Yes, right click your sheet tab, view code, and paste my routines in a test worksheet's object. You can then try some tests for single clicks and doubleclicks. You have to be really fast at doubleclicking to get that event to fire rather than the selection event.

    Target.Cells.Count is the same as Selection.Cells.Count FWIW.

    Notice how I used the address property to do a bit more in the beforedoubleclick event if cell A5 was doubleclicked. Normally, one uses Intersect() to intersect the Target and a set of cells to take some action. Select Case of an intersected range can be useful as well. It all depends on how complex you need to get to meet your needs. A search of Intersect on the forum will show some code examples.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •