VB: vba action for simulate: Click a Cell

Anonymous
2016-03-09T15:53:00+00:00

how to execute a mouse function?  i do not need an execute if click on a cell.  i need a function that simulates the click itself.

i would like the hyperlink in the cell to activate from a vb simulated click.

i have an answer that does a split & follow hyperlink, but that is not what i need.

for other vb i had:    application.doubleclick  work

is there a way to make the vb perform a single click on the selected cell.  thanks.

i tried a couple of these:

    application.singleclick    'no such animal

    application.click

    click

i do not need a mouse click event.

i need the vb to perform a click on a selected cell.

==========

i can save another post on a similar problem:

how do i limit a vb to performing the task only once / for one mouse click.  the problem would be if 2 clicks were accidently performed.

eg:  i do not want 2 saves workbooks to take place, when i hit a "Save Workbook" macro - button.  thanks.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. HansV 462.3K Reputation points MVP Volunteer Moderator
    2016-03-09T16:10:20+00:00

    Let's say the cell is A1 on Sheet1.

        With Worksheets("Sheet1").Range("A1").Hyperlinks(1)

            ActiveWorkbook.FollowHyperlink .Address, .SubAddress

        End With

    If you want to follow the hyperlink in the currently active cell, use ActiveCell instead of Worksheets("Sheet1").Range("A1").

    0 comments No comments
  2. Anonymous
    2016-03-09T16:59:54+00:00

    hi,  i tried that and a few others - checking, below.

    am not that good at vb & don't know if i am missing some small item.  (if matters, doing alot of work and trying to be able to upgrade, but am on: xp).

    note:  would have something for a 1 line call, like:  goClick()

    one funny thing found  that works for right click below:  sendkeys f10

        'With ActiveCell.HYPERLINKS(1)   'line err:  subscript out of range

        With Worksheets("Sheet1").RANGE("BM1893").HYPERLINKS        'same err below: argument not optional: .FollowHyperlink

        'With Worksheets("Sheet1").RANGE("BM1893").HYPERLINKS(1)

        'With Worksheets("Sheet1").RANGE("A1").HYPERLINKS(1)

            ActiveWorkbook.FollowHyperlink.Address , .SubAddress    'argument not optional: .FollowHyperlink    (deleted space before .address)

        End With

        'ActiveWorkbook.FollowHyperlink.Address , .SubAddress    'invalid reference:  .SubAddress

        'MOUSE.Click = "left"    'i do not want to detect a mouse click, i want to perform one.

        'If MOUSE.Click = "LEFT" Then

            'ans ck:  http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/

        'SendKeys ("+{F10}")   'send right click to application?  then do tabs..  YES but not left click

        'Click

        'DoubleClick

        'Application.DoubleClick

        'Selection.Click

    0 comments No comments
  3. HansV 462.3K Reputation points MVP Volunteer Moderator
    2016-03-09T17:16:38+00:00

    Try this instead:

    ActiveWorkbook.FollowHyperlink    Worksheets("Sheet1").Range("A1").Hyperlinks(1).Address

    or if you want to refer to the active cell:

    ActiveWorkbook.FollowHyperlink    ActiveCell.Hyperlinks(1).Address

    0 comments No comments
  4. Anonymous
    2016-03-09T18:12:14+00:00

    hi,  i tried the following, but no luck.  not a critical item, but am interested if there is an answer in that.

    'Sub Test1()

    ActiveWorkbook.FollowHyperlink ActiveCell.HYPERLINKS(1).Address   'activecell,   line err: subscript out of range

    'ActiveWorkbook.FollowHyperlink Worksheets("Sheet1").RANGE("A1").HYPERLINKS(1).Address

    'end sub

    if a matter of hyperlink uses a friendly name problem..  checking,  not work either.  else old examples of follow hyperlink might do that..  was:

        'ActiveWorkbook.FollowHyperlink ActiveCell   '(after remove friendly name from hyperlink, this works)

    i had help before, receiving a macro for splitting a unique hyperlink friendly name out.  i wondered if there wasn't something more general so to escape identifying the friendly name..  but then again, that option was conveniently included in the user defined function?  i can inlcude that here,  but note:

    NOTE:  might just need original request for left click.

    OTHER VB that works well,  splits friendly name out.  (even so,  looking for a single click vb..)

    Sub goFHL(strF As String, strDL As String)        'GOFHLXXX gofhlxxx  255 chars AS:  goFHL ActiveCell.Formula, ",friendlyname"

        Dim V As Variant                              'follow HYPERLINK AS: (left of friendly name):  goFHL ActiveCell.Formula, ",IF("

        'enter quotes as string ANS:  either double up on quotes:

        'goFHL ActiveCell.Formula, ",""SAM"""         'for: ,"SAM"    OR USE:  & CHR(34) &

        'goFHL ActiveCell.Formula, "," & CHR(34) & "SAM" & CHR(34)

        If InStr(1, strF, "HYPERLINK") = 0 Or InStr(1, strF, strDL) = 0 Then

          Application.EnableEvents = True: Exit Sub         'EVENTS

        End If

        V = split(strF, strDL)

        On Error Resume Next

        ActiveWorkbook.FollowHyperlink Application.Evaluate(Replace(V(0), "HYPERLINK(", ""))

        Application.EnableEvents = True               'EVENTS

    'gofhl() GOFHL()

    End Sub

    ==========

    OTHER:  i found examples of below,  but as yet unable to apply.  not sure what to put where.

    'orig:

    'Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

    'Const MOUSEEVENTF_RIGHTDOWN = &H8

    'Const MOUSEEVENTF_RIGHTUP = &H10

    'Public Sub RightDown()

    'mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0

    'mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0

    'trying:

    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

    Const MOUSEEVENTF_LEFTDOWN = &H2

    Const MOUSEEVENTF_LEFTUP = &H4

    Public Sub goClick()   

    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0

    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

    End Sub

    OTHER:

    sites that seem to have a similar examples,  still did not focus on the one item,  perform a click  and/or how to apply it?  will check later.

    http://www.myengineeringworld.net/2011/11/handle-other-applications-through-excel.html

    http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/

    a google might be

    https://encrypted.google.com/search?complete=0&num=100&hl=en&safe=off&q=vba+xp+excel+left+mouse+click+%22Private+Declare+Sub+mouse\_event+Lib%22

    http://www.ozgrid.com/forum/showthread.php?t=188084

    with these examples,  if possible, would like a simple call to click the selected cell  (but not going to exclude examples for selecting other locations/ remotely) for something merely like:

        call goClick  'to left click the current selection

    0 comments No comments
  5. HansV 462.3K Reputation points MVP Volunteer Moderator
    2016-03-09T20:29:40+00:00

    Your original description implied that the cell in question contained a clickable hyperlink. The error message "Subscript out of range" suggests that the cell does NOT contain a hyperlink, however.

    If the cell contains a web address but not as a hyperlink, you can try

    ActiveWorkbook.FollowHyperlink    ActiveCell.Value

    0 comments No comments