While Navigating my sheet I use the F5+[enter] to navigate back to the cell that I hyperlinked from on a different sheet in the same workbook.
I have now made an Activex button to act as a back button but I need the script to make it do an F5+[enter] when clicked on.
I have lots of hyperlinks from different areas that go to one specific sheet and I basically want the Activex button to return the cursor back from whence it came.
Any help would be appreciated
Thank you
*****Sorry My fault I meant Active X Button*****
Edited and re tagged. Much appreciated
Since application.goto function stores the last reference as default you can simply bind a key for the following command
Application.Goto
This is not the perfect solution but it will do what you want. All you need to do is add this line to button_click.
As a general note, mimicking the actions of the keyboard through VBA involves using the Application.SendKeys command. In your specific case, the code would be:
Application.SendKeys "{F5}"
Application.Wait (Now + TimeValue("00:00:01"))
Application.SendKeys "~" 'The tilde is the Enter key
Generally, I prefer not to use this methodology. On my computer, for example, the third line of code runs before the first line has completed its action. In that case, I'm sitting with the Go To dialog box on my screen. That's why I separate the two lines with a call for the system to delay processing.
EDIT: (I think I cut off part of my original post)
In my opinion, your best bet is to allow Excel to handle the flow back to the hyperlink cells. The also opens up the possibility to move back beyond just the last cell. Below is a loose example of the code, which should be adapted.
In a module, enter the below code:
Public strLastSheet(1 To 10) As Worksheet
Public strLastRange(1 To 10) As Range
Public nLastCell As Integer
Public nCurrentIndex As Integer
Sub SelectLastHyperlinkedCellFoShizzle()
strLastSheet(nCurrentIndex).Activate
strLastRange(nCurrentIndex).Select
If nCurrentIndex <= 1 Then
'Do Nothing
Else
nCurrentIndex = nCurrentIndex - 1
End If
End Sub
In the ThisWorkbook code, enter the following:
Private Sub Workbook_Open()
nLastCell = 0
nCurrentIndex = 0
End Sub
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
nLastCell = nLastCell + 1
Set strLastSheet(nLastCell) = Sh
Set strLastRange(nLastCell) = Target.Parent
nCurrentIndex = nLastCell
End Sub
This is set to store the previous 10 hyperlink addresses. It should be further modified to disallow entries beyond 10 into the array, and perhaps shift the previous entries down the array.
Related
I am trying since 2 days to find how to do the following without finding anything that suits the aim:
Steps by order :
user open excel file
he chose between folowing :
Paste an image directly in the worksheet (may be an limited area)
activate some video in the workbook (may be a webcam for start)
he select with a button to activate his clicks detection
he clicks anywhere on the picture and i get the coordinates of clicked points
So far i've seen ppl using (and tested myself) :
mouse event ==> this does not work as i need to know the name of what he is clicking on and it may be a brand new picture he just pasted
BeforeDoubleClick (same, i'd prefer avoid doubleclick but even then it doesnt work when i click on something else but cells)
Selectionchange ==> doesnt work if im not clicking on cells
Place hidden button over the area i want : i cant click a button if its not visible, and it becomes visible when i click it if i put as transparent
If anyone has ideas about this...
(nb: im not a pro of vba so i may have missed something)
Just forgot : my issue is not getting the coordinates of mouse, its just triggering the macro when i want, for now im jsut trying to get a simple msgbox to see if trigger works.
Thanks if anyone has any ideas
BR
Not sure if this fits your need (for example couldn't test it with a video).
a) Place a "button" of any kind on your sheet. I usually use a square shape for that and format it a little bit (color, shade, text). Assign the subroutine SetEvents to it.
b) Declare a global variable that remembers that click-activation is active
Option Explicit
Global EventCatchingActive As Boolean
c) In the event routine of your button, set the OnAction-method for all shapes of the sheet - see the routine setEvents. This ensures that even newly added images handle the click event.
Sub setEvents()
' This routine is called from the magic button.
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1) ' Set this to whatever sheet you need
Dim button As Shape
Set button = ws.Shapes(Application.Caller)
If EventCatchingActive Then
EventCatchingActive = False
button.TextFrame2.TextRange.Characters.Text = "Start Clicking"
Else
Debug.Print "Setting EventHandler"
Dim sh As Shape
For Each sh In ThisWorkbook.Sheets(1).Shapes
' Debug.Print sh.Name, sh.Type
If sh.Name <> button.Name Then sh.OnAction = "ClickedMe"
Next
EventCatchingActive = True
button.TextFrame2.TextRange.Characters.Text = "Stop Clicking"
End If
End Sub
d) Declare a routine that is called if any of the shapes is clicked. With Application.Caller you can check what was clicked.
Sub ClickedMe(Optional target As Range = Nothing)
If Not EventCatchingActive Then Exit Sub
If target Is Nothing Then
Debug.Print "I clicked on " & Application.Caller
Else
Debug.Print "I clicked on cell " & target.Address
End If
End Sub
(note that code of steps b) to d) goes into a regular module)
e) If you also want to handle clicks on a cell, add the following into the sheet-module of the worksheet you are dealing with.
Private Sub Worksheet_SelectionChange(ByVal target As Range)
ClickedMe target
End Sub
I don't like excels default of following hyperlinks when clicking on cell. I'd like to adjust it so that you have to press CTRL while clicking the cell to follow hyperlink.
I found this post on SU https://superuser.com/a/739899/465645 which has a great idea, but uses "formula" and in the comments there was this:
# Engineer Toast
I think VBA is the only way to do it. I would propose that you don't
have to use the HYPERLINK function, though. You could also use
Target.Hyperlinks(1).Address (with error trapping) to determine if the
cell has a hyperlink
My hyperlinks are not in the "formula" fashion stated in the answer, but I love the general idea. Is there a way I can use a class module with app level events to watch for when cells are clicked and contain a hyperlink and see if user has CTRL pressed or not and follow hyperlink accordingly?
Bonus: How could I adjust the text that appears when you hover over a hyperlink to state accurately that CTRL + Click opens hyperlink.
Answer from SU:
How I do this: Set the hyperlink with the URL in the second portion:
=HYPERLINK("","http://example.com")
In the VBA Editor make a module:
Declare Function GetKeyState Lib "User32" (ByVal vKey As Integer) As Integer
Global Const CTRL_KEY = 17
Then the worksheet code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Error1
If Left(Target.Formula, 10) = "=HYPERLINK" Then
If GetKeyState(CTRL_KEY) < 0 Then 'Check if CTRL is held in
Application.EnableEvents = False
ThisWorkbook.FollowHyperlink Mid(Target.Formula, 16, Len(Target.Formula) - 17)
Application.EnableEvents = True
End If
End If
Exit Sub
Error1:
Application.EnableEvents = True
End Sub
Update: I have an attack plan, but it will take some time for me to get it sorted. It's impossible to "trap" a hyperlink click before the link is opened. So, the only way for this to work is for me to convert all hyperlinks in a page to have the hyperlink in the "second part" then set the actual hyperlink to a reference to the cell itself and then I can get the above code modified.
I have knowledge of the RANDBETWEEN() function inside the cells of Excel. This only returns one (1) value. I would like to assign this to a button to return a different value every time the button is hit.
I can create the button, I have the cell. How can I link the two?
Long story short, every time a button is clicked a cell (lets say cell a1) gets a different value between values a to b.
Try below
Private Sub CreateRandom()
'random number between 11 and 100
Debug.Print NewRandom(11, 100)
End Sub
Private Function NewRandom(ByVal FromLimit As Integer, ByVal ToLimit As Integer) As Integer
Randomize
randomNumber = Int((ToLimit - FromLimit) * Rnd) + FromLimit
NewRandom = randomNumber
End Function
Private Sub CommandButton1_Click()
'random number between 30 and 100
Sheets("Sheet2").Range("A1").Value = NewRandom(30, 100)
End Sub
MI Consol:
Sheets:
Private Sub CommandButton1_Click()
Call p
End Sub
Private Sub CommandButton3_Click()
Call save
End Sub
Private Sub CommandButton4_Click()
Call EMail
End Sub
Private Sub CommandButton5_Click()
Call iSMS
End Sub
Private Sub CommandButton6_Click()
Call continue
End Sub
Private Sub CommandButton7_Click()
Call Continue1
End Sub
If you're still using the RANDBETWEEN function and you just want it to generate a new value on command, just press F9 to force Excel to recalculate all formulas. RANDBETWEEN will then generate a new random number. (If it doesn't change value, it's just because it generated the same random number by chance. Keep pressing F9 to get new random numbers.)
Note that changing any formula on the spreadsheet will also cause the RANDBETWEEN function to generate a new value.
If you want a new random number to be generated ONLY when you command it to, then you do need to create a macro, which can be easily called through a button. I'm assuming you're not familiar with macros at all, so the following is a simple solution.
The following website explains how to create a simple button that will run a macro. (Note that this specific method only works with the ActiveX button, not the Forms button.)
https://www.tutorialspoint.com/vba/vba_excel_macros.htm
You will just need to replace the code in their example (MsgBox "Hi") with the following code:
Range("A1") = WorksheetFunction.RandBetween(a,b)
Replace a and b with the numbers you want to get a random number between. You can do this a few ways:
If you always want it between the same two numbers (e.g. 1 and 10), you can hard-code them into the macro:
Range("A1") = WorksheetFunction.RandBetween(1,10)
If you want it between two numbers recorded on the same spreadsheet (e.g. in cells B1 and B2), you can refer to the cells:
Range("A1") = WorksheetFunction.RandBetween(Range("B1"),Range("B2"))
Or you can ask for the numbers in popups:
a = InputBox("Enter a")
b = InputBox("Enter b")
Range("A1") = WorksheetFunction.RandBetween(a, b)
Note that the website misses a couple of important steps you must perform before the button will be able to used:
Close the VBA window (not actually necessary, but there's no need to leave it open)
Exit Design Mode. On the Developer tab in Excel, the Design Mode button should still be highlighted. Click it to un-highlight it. You can now click your button.
Note that if you want to change the code for the button again, you need to click Design Mode again to highlight it again, then you can double click the button to view the code again.
Google should provide me with ample examples but none of them seem to work
What I want: Everytime the user presses, and then releases, the ENTER key, for my program to do do something (ie. create a MsgBox, or call function Foo). I would prefer this in the form of a MWE
What I have done: I have tried googling it but none of the examples are functional. They compile, but don't do anything. I have also made sure to save in a macro compatible Excel format.
What I am using: I am using Excel 2016, 64 bit with Office 365
EDIT: The user is entering this information into the worksheet. I want to intercept the user input and everytime they press ENTER, take the cursor/active cell down two rows, so there is an empty cell below every cell. If the user presses tab, I want to take the cursor/active cell right two columns, so there si an empty cell to the right of every cell.
EDIT 2: here is a MWE of what I have right now which should work, but which does nothing. I am adding this to the worksheet, and not as a module
Sub SomeActions()
MsgBox ("Hello")
End Sub
Private Sub Workbook_Open()
Application.OnKey "~", "SomeActions"
End Sub
First, make an callback Sub that performs the logic you need. Put it into a new code module (NOT into worksheet code):
Sub SomeActions()
...
End Sub
Then, subscribe to OnKey event, for example, when the user opens the workbook (this code goes into ThisWorkbook) module in VBA editor:
Private Sub Workbook_Open()
Application.OnKey "~", "SomeActions"
End Sub
"~" means Enter key. For numeric keypad key use "{ENTER}".
What I want: Everytime the user presses, and then releases, the ENTER key, for my program to do do something (ie. create a MsgBox, or call function Foo). I would prefer this in the form of a MWE
If I read this correctly, you want a blank row between every user inputted value doing down and a blank column between every user inputted value going right.
On the worksheet code sheet:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
Dim r As Long, c As Long
If Target.Row / 2 <> Int(Target.Row / 2) Then r = 1
If Target.Column / 2 <> Int(Target.Column / 2) Then c = 1
Target.Offset(r, c).Activate
Application.EnableEvents = True
End Sub
This occurs for all navigation. If you only want this to occur on input, modify to suit a Worksheet_Change event macro instead of a Worksheet_SelectionChange event macro.
Google should provide me with ample examples but none of them seem to work
What I want: Everytime the user presses, and then releases, the ENTER key, for my program to do do something (ie. create a MsgBox, or call function Foo). I would prefer this in the form of a MWE
What I have done: I have tried googling it but none of the examples are functional. They compile, but don't do anything. I have also made sure to save in a macro compatible Excel format.
What I am using: I am using Excel 2016, 64 bit with Office 365
EDIT: The user is entering this information into the worksheet. I want to intercept the user input and everytime they press ENTER, take the cursor/active cell down two rows, so there is an empty cell below every cell. If the user presses tab, I want to take the cursor/active cell right two columns, so there si an empty cell to the right of every cell.
EDIT 2: here is a MWE of what I have right now which should work, but which does nothing. I am adding this to the worksheet, and not as a module
Sub SomeActions()
MsgBox ("Hello")
End Sub
Private Sub Workbook_Open()
Application.OnKey "~", "SomeActions"
End Sub
First, make an callback Sub that performs the logic you need. Put it into a new code module (NOT into worksheet code):
Sub SomeActions()
...
End Sub
Then, subscribe to OnKey event, for example, when the user opens the workbook (this code goes into ThisWorkbook) module in VBA editor:
Private Sub Workbook_Open()
Application.OnKey "~", "SomeActions"
End Sub
"~" means Enter key. For numeric keypad key use "{ENTER}".
What I want: Everytime the user presses, and then releases, the ENTER key, for my program to do do something (ie. create a MsgBox, or call function Foo). I would prefer this in the form of a MWE
If I read this correctly, you want a blank row between every user inputted value doing down and a blank column between every user inputted value going right.
On the worksheet code sheet:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
Dim r As Long, c As Long
If Target.Row / 2 <> Int(Target.Row / 2) Then r = 1
If Target.Column / 2 <> Int(Target.Column / 2) Then c = 1
Target.Offset(r, c).Activate
Application.EnableEvents = True
End Sub
This occurs for all navigation. If you only want this to occur on input, modify to suit a Worksheet_Change event macro instead of a Worksheet_SelectionChange event macro.