+ Reply to Thread
Results 1 to 8 of 8

Run A Macro By Clicking A Specific Cell In Excel

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-28-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365
    Posts
    298

    Exclamation Run A Macro By Clicking A Specific Cell In Excel

    I found the macro below which works perfectly if you just want to click in one cell.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Selection.Count = 1 Then
            If Not Intersect(Target, Range("P31")) Is Nothing Then
                Call Macro1
            End If
        End If
    End Sub
    The problem is I want to run different macros if I click different cells. I thought I could combine all of them in one as per below, but it didn't work.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Selection.Count = 1 Then
            If Not Intersect(Target, Range("P31")) Is Nothing Then
                Call Macro1
            If Not Intersect(Target, Range("Q10")) Is Nothing Then
                Call Macro2
            If Not Intersect(Target, Range("U25")) Is Nothing Then
                Call Macro3
            End If
        End If
    End Sub
    Any ideas?

    Thanks,

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,469

    Re: Run A Macro By Clicking A Specific Cell In Excel

    .
    Select case ??

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,469

    Re: Run A Macro By Clicking A Specific Cell In Excel

    .
    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
     If Selection.Count = 1 Then
            If Not Intersect(Target, Range("P31")) Is Nothing Then
                Call Macro1
            ElseIf Not Intersect(Target, Range("Q10")) Is Nothing Then
                Call Macro2
            ElseIf Not Intersect(Target, Range("U25")) Is Nothing Then
                Call Macro3
            End If
        End If
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-28-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365
    Posts
    298

    Re: Run A Macro By Clicking A Specific Cell In Excel

    It works well, but how can I add a error message if there is an error?

    I was think something like the below in red but it didn't work.

    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
     If Selection.Count = 1 Then
            If Not Intersect(Target, Range("P31")) Is Nothing Then
                Call GotoLeadTime
            ElseIf Not Intersect(Target, Range("Q10")) Is Nothing Then
                Call GotoStock
            ElseIf Not Intersect(Target, Range("U25")) Is Nothing Then
                Call GotoCustomers
            Else
            MsgBox "No match"
            End If
        End If
    End Sub

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,469

    Re: Run A Macro By Clicking A Specific Cell In Excel

    .
    Your macro works here, if you are double-clicking in any cell other than those listed in the macro.

    Do you intend for it to do something different ?
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    07-28-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365
    Posts
    298

    Re: Run A Macro By Clicking A Specific Cell In Excel

    The macro works well, the problem is different: the tab I run this macro has an item, and when I click on these three different cells, they run macros they will take me to this item in different tabs, such as sales or stock tabs. Sometimes this item does not exist in one of these tabs, so I have a macro error.

    Perhaps what I need to do is to fix the macros that are being called. One of them is the one below:
    Sub GotoStock()
        ItemRow = Sheets("Stock").Range("A:A").Find(what:=Sheets(" P S I ").Range("C7")).Row
        Application.GoTo Sheets("Stock").Cells(ItemRow, 1)
    End Sub
    How can I add a message box, saying something like "not found" if the macro doesn't find what it is looking for?

  7. #7
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,469

    Re: Run A Macro By Clicking A Specific Cell In Excel

    .
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
     On Error GoTo Err:
     
     If Selection.Count = 1 Then
            If Not Intersect(Target, Range("P31")) Is Nothing Then
                Call GotoLeadTime
                
            ElseIf Not Intersect(Target, Range("Q10")) Is Nothing Then
                Call GotoStock
                
            ElseIf Not Intersect(Target, Range("U25")) Is Nothing Then
                Call GotoCustomers
            Else
            MsgBox "Nothing found !"
            End If
    End If
    
    Exit Sub
    
    Err: MsgBox "An error occured and may include Nothing Found"
    
    End Sub

  8. #8
    Forum Contributor
    Join Date
    07-28-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365
    Posts
    298

    Re: Run A Macro By Clicking A Specific Cell In Excel

    The error message box works fine, but I had an idea. Is it possible to add a command in the beginning of the macro below, to clear the filter (not to remove, just to clear) in the "Stock" sheet?

    90% of the times the error message pops up is because the Stock sheet has the filter on, so if we clear the filter that should fix most of problems.

    Sub GotoStock()
        ItemRow = Sheets("Stock").Range("A:A").Find(what:=Sheets(" P S I ").Range("C7")).Row
        Application.GoTo Sheets("Stock").Cells(ItemRow, 1)
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Run specific macro when clicking shape based on cell value
    By aimone111 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-27-2017, 06:34 AM
  2. how to add values under specific columns by clicking on button in excel macro
    By DURGESH88 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2015, 12:00 AM
  3. Replies: 2
    Last Post: 08-13-2014, 06:39 PM
  4. Replies: 1
    Last Post: 08-13-2014, 05:46 PM
  5. Clicking email links to send specific cell information
    By Dessesbo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-24-2013, 10:36 AM
  6. Replies: 1
    Last Post: 06-30-2013, 05:58 PM
  7. Clicking a cell should trigger a MsgBox with specific details
    By ssdlfun in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-05-2010, 02:13 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1