I am trying to access my workbook and sheets while I am in a subroutine of my UserForm.
My macro starts when the focus in the worksheet is changed:
Sub Worksheet_SelectionChange(ByVal Target As Range)
'...
End Sub
In my UserForm, I now wanted to define what happens when the red x on the top right is pressed. In that case I want to change the focus/selection in Sheet1 when that UserForm is closed. Though, I want to do this relative to the original position, so relative to Target or relative to the ActiveCell.
What works in the routine above is:
ActiveCell.Offset(0, 1).Select
However, I would like to do this in the routine below:
Sub UserForm_Terminate()
Me.Hide
'change focus here
End
End Sub
I was able to select a cell with
Sheet1.Cells(3, 3).Select
but that is obviously not relative to the original position. Neither Target nor ActiveCell... are available in UserForm_Terminate
I hope anyone can help
Vincenz
I'm not sure I fully understand what you're trying to achieve, but can't you simply use:
Application.ActiveCell
in UserForm_Terminate?
Related
I've got two buttons on a worksheet that I've named "RemoveButton" and "AddButton". I've also added hyperlinks to both shapes and both shapes will point to the same cell once clicked. When I click both buttons, they point to cell A1 as expected, but the FollowHyperlink code does not recognize that a hyperlink has been clicked.
I wanted to use the FollowHyperlink worksheet event to recognize the shape that is clicked. I created the macro as below:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Debug.Print "Clicked!"
End Sub
When clicking on the shapes, they just point to A1 and "Clicked!" never shows in my Immediate window. However, I created a test hyperlink that is text only and when selected, "Clicked!" appears. This indicates that Excel isn't treating the buttons as hyperlinks even though they have hyperlinks added to them.
The reason for the hyperlinks on the shape is for them to run code. I could use the assign macro feature to the shape, but in doing so I wouldn't be able to add a ScreenTip to the shape. I really want the ScreenTip as this will help future users know what the button is for.
Can someone please help me understand if this is possible?
Screenshot of buttons
There is a workaround for this problem. Instead of the Worksheet_FollowHyperlink event, you can use the Worksheet_SelectionChange event.
To do this, you need a cell that is completely covered up by your button. If the button is too small to cover up a cell, you can just hide a row and a column and place the button at the intersection of the hidden row and column.
Now, we link the button with the "hidden" cell, C5 in this example:
Now the hidden cell can only be selected by clicking the button.
So if the Target in the Worksheet_SelectionChange event is the cell C5, we know that the button has been clicked.
To leave the previous selection unaffected, you can use the following code in the worksheet's code module:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const HIDDEN_CELL_ADDESS As String = "$C$5" '<--Set hidden cell address here
Static previousSelection As Range
If Target.Address = HIDDEN_CELL_ADDESS Then
'Make sure the linked cell doesn't stay selected, otherwise the next
'click on the button may not be recognized
Application.EnableEvents = False
If Not previousSelection Is Nothing Then previousSelection.Select
If TypeName(Selection) = "Range" Then
If Selection.Address = HIDDEN_CELL_ADDESS Then Target.Offset(1).Select
End If
Application.EnableEvents = True
Call ShapeClicked
Else
Set previousSelection = Target
End If
End Sub
Sub ShapeClicked()
MsgBox "The button has been clicked"
End Sub
GWD's answer correctly solves the issue. I also found a slightly different way to work around this as well. Please see below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B11")) Is Nothing Then
Call dispatchLink(Target.Address)
End If
End Sub
I have an Excel WorkBook with several Sheets. I would like to be able to select from a drop down list in the "Home" Sheet and after selection is made, automatically switch to the proper Sheet and select a specific Cell.
It appeared to be easy, but I have failed time and again to make it work.
Here is an example of what I would like to do:
The only code that I managed to have a little success with is the following:
Private Sub Worksheet_Activate()
With Sheets("Home")
If Cells(6, 3).Value = "A" Then
Sheets("A").Select
ActiveSheet.Range("B7").Select
End If
End With
End Sub
The problem with it is that it will not check for the value until the user moves to another sheet. Then when it comes back, it will check for it, and will take it to the correct one, but the user will be stuck in a loop without being able to go back to "Home". (I know that it will only work on cell C6, but I just wanted to try if it worked before changing the Range)
You need the worksheet change event, rather than activate. Try this, in the Home sheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.Goto Sheets(Target.Text).Range("B7")
End If
End Sub
I'm working in Excel. I'd like to press a button to cancel the value of the cell at the left of the button itself.
So the user experience should be the following:
When the user presses "button1", the cell at its left became 0. The same for "button2" and "button3"
How can I do?
By using a Macro?
Assign this macro to any and all buttons, and it'll delete the info. in the cell directly to the left.
Sub test()
Dim btnRow&, btnCol&
btnRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
btnCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
Cells(btnRow, btnCol).Offset(0, -1).ClearContents
End Sub
Or, thanks to #Rory, this can be:
Sub test()
Activesheet.Shapes(Application.Caller).TopLeftCell.offset(0,-1).ClearContents
End Sub
Note: Make sure your shapes are well placed, as this uses wherever the top left of the shape is to determine the row/column. This macro reduces the need to run three different ones, depending on where, and minimizes any If/Then type statements as it uses the Caller to determine which shape, which determines where it is.
May this help...
'Add three buttons on the sheet
'And imaging that you want to delete B4 B5 B6
'You can discover which button is, by double cliking on the desing mode
'on your excel
Private Sub CommandButton1_Click() 'This is the button on the cell C4
Range("B4").ClearContents
'Here B4 is the range of the cell you want to delete...
'This could be acomplish just pressing the button DELETE
'Same by the two other buttons... just adding 1 to the number
'B4...B5...B6
'With this you only can delete de contents of the cell...
End Sub
Private Sub CommandButton2_Click() 'This is the button on the cell C5
Range("B5").ClearContents
End Sub
Private Sub CommandButton3_Click() 'This is the button on the cell C6
Range("B6").ClearContents
End Sub
Hyperlink method is what I've done before. Like it, because it look like it can click.
Add below code on the Worksheet module, when a hyperlink clicked, it will trigger the sub
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If LCase(Target.Range.Text) Like "delete*" Then
ActiveCell.Offset(0, -1).ClearContents
End If
End Sub
You can use below code to generate as much as hyperlink you want.
Sheets(XXX).Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"", TextToDisplay:="Delete"
If your using command button
Option Explicit
Sub Button1_Click()
Range("A1") = 0
End Sub
or assign to worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then _
Range("A1") = 0
End Sub
I need to have a macro executed when someone clicks on the shape in the worksheet which is a picture. The macro intention is to display more information regarding the shape on which the user clicked.
EDIT: I also needed an information display during mouse-rollover, hence used hyperlink method.
I followed the method outlined in the MSDN , but the macro just doesn't seem to run.
My Macro is as follows:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
MsgBox "Source: Target.Range.Address " & Target.Range.Address
MsgBox "Source: Target.Range.Value " & Target.Range(1, 1).Value
' Some more macro stuff here
End Sub
On clicking the picture I get to the sheet where the hyperlink target
is, but the macro doesn't get executed.
I created a dummy hyperlink on one of the cells (in the same sheet), if I click that one the macro is executed.
I used the hyperlink method to have the rollover information on the picture shape.
I am totally out of ideas about how to go about this. Help would be greatly appreciated.
Don't use that method. Use Worksheet_SelectionChange instead of Worksheet_FollowHyperlink.
So change your code to
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'~~> Change A1 to the cell which is behind the shape
If Not Intersect(Target, Range("A1")) Is Nothing Then
'~~> Change this to the relevant sheet
With ThisWorkbook.Sheets("Sheet1")
.Visible = xlSheetVisible
.Activate
End With
'
'~~> Other Code if required
'
End If
End Sub
Next, Right Click on the Shape and click on Hyperlink and hyperlink to a cell behind the shape. You may also display information using the ScreenTip button in the Insert hyperlink dialog box.
We will do the sheet activating part in the above code rather than letting Excel do it.
Hope this solves your issue.
I was wondering how can i one use VBA/macros to lock certain excel cells that are selected/highlighted by the user.
The code im using right now is locking the entire sheet.
Sub Macro4()
'
' Macro4 Macro
'
'
Worksheets("Sheet1").Activate
ActiveSheet.Unprotect
Cells.Select
Selection.Locked = True
ActiveSheet.Protect
End Sub
Any ideas on what im doing wrong?
Thank you for your time.
If you want to perform any actions on the selected cell(s) every time a new selection occurs, you should rely on the code being triggered when this happens:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Locked = True
End Sub
This inside the file with the code for the given sheet; that is, if you want to consider Sheet1, the file where you have to write this code is: Microsoft Excel Objects/Sheet1 (Sheet1).
UPDATE AFTER YOUR COMMENT
Sub Button1_Click()
Selection.Locked = True
End Sub
This code locks all the cells selected when the Button1 is clicked.