I have a group of cells in my worksheet that use a data validation list. It works great, but I'd like the list to dropdown automatically when one of these cells gets the focus. I found some code online to make this work.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Worksheet.Name = "Sheet1" And Target.Column = 1 Then
Application.SendKeys ("%{down}")
End If
End Sub
Courtesy of: http://howoffice.com/automatically-open-the-drop-down-list-when-a-cell-get-focus-in-excel/
However, the issue I'm having is with the SendKeys causing the NumLock to toggle on and off. I looked this up and it's a known issue with the command.
Are there any other alternatives to the SendKey command to accomplish what I'm looking to do here?
Any help is appreciated.
This code works for me:
Courtesy of: On focus after tabbing of Excel drop down, automatically show list for selection
Private Sub Worksheet_SelectionChange(ByVal Target As Range
On Error GoTo Err1:
If Target = Range("I5") Then
Application.SendKeys ("%{UP}")
End If
If Target = Range("I6") Then
Application.SendKeys ("%{UP}")
End If
If Target = Range("I7") Then
Application.SendKeys ("%{UP}")
End If
Err1:
'do nothing
End Sub
And here's the code with SDB MadDog's corrections:
If you want it to apply to all cell's with data validation
( 'On focus after tabbing of Excel drop down, automatically show list for selection )
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Err1:
If Target.Cells.Count = 1 Then
If Target.Validation.InCellDropdown = True Then
Application.SendKeys ("%{UP}")
End If
End If
Err1:
'do nothing
End Sub
However, it changes my NUMLOCK status (toogle ON / off) everytime!
He mentioned he fixed it with an API, I tried, but it doesn't work because it can't get numlock status read correctly (this one http://www.freevbcode.com/ShowCode.asp?ID=1151)
But hope this helps someone in need!
this code works for me :
SendKeys "%{down}", True
DoEvents
SendKeys "{SCROLLLOCK}"
Related
I am trying to write a code using Macro, but with no luck.
The task can be simply defined as, when the user enters a Serial Number such "AB123" anywhere in column I, a list should appear automatically in columns in (J, K, L). please the the attached picture
If there is any other way to do that without using Macro, I am glad to hear it.
Thank you in advance, I hop that I make myself clear.
The pic. shows whenever the RED Serial Number is entered. The Highlighted columns in green should appear.
Something for you to think about. Put this code in your Sheet. Double-click the sheet name and a windoe to enter code will appear.
Option Explicit
Public Sub Worksheet_Activate()
Me.Range("J:L").EntireColumn.Hidden = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("I:I")) Is Nothing Then
If Target.Cells.CountLarge = 1 Then
If Target.Value = "AB1234" Then
Me.Range("J:L").EntireColumn.Hidden = False
Else
Me.Range("J:L").EntireColumn.Hidden = True
End If
Else
'Nothing
End If
End If
End Sub
Private Sub Worksheet_Deactivate()
Me.Range("J:L").EntireColumn.Hidden = False
End Sub
I am currently trying to put a hyperlink in one overarching tab that would trigger an autofilter in another tab once clicked. I've put in what i believe to be the correct macro for the hyperlink but when clicked nothing happens. I even tried it on a new Excel document and the code works fine. Is there something I could update in my document to allow hyperlinks or is my code incorrect.
Thanks!
In-Sheet code:
Private Sub Worksheet_Hyperlink(ByVal Target As hyperlink)
If Target.Range.Address = "$F$5" Then
Call hyperlink2
End If
End Sub
Reference Code:
Sub hyperlink2()
Application.ScreenUpdating = False
Worksheets("NJ Reporting Template").Activate
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Range("Z6").Select
ActiveSheet.Range("$N$6:$Z$150").AutoFilter Field:=26, Criteria1:="Yes"
Application.ScreenUpdating = True
End Sub
I've read many threads on this topic, but the code I have found there doesn't seem to work. I am trying to disable the "cut" function from an excel spreadsheet and I would like the icon to grey out.
I have been using this code:
Sub WorkSheet_Activate()
Application.CommandBars.FindControl(ID:=21).Enabled = False
End Sub
However, I can still use the "cut" function without any problem....
Also I know there are some functions that allow you to disable cut/copy/paste, but I still want copy and paste to be allowed in this spreadsheet.
Thanks in advance for your help!
Kristen
Check the link for the reference - Disable Cut
Try something like this:
Option Explicit
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please DO NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select
End Sub
Hope it helps.
I have an excel worksheet that is being used as a database front end for access. When a user changes data in a cell it will run the Worksheet_changed event to run codes that updates the access database.
However - I also have a refresh button that, you guessed it, refreshes the spreadsheet. This also causes the worksheet_changed event to run which will sometimes error out the program.
Private Sub RefreshButton_Click()
Refreshbuttons
ActiveWorkbook.RefreshAll
End Sub
How do I stop the work sheet changed event from happening when the refresh button is pressed? I have tried a Boolean flag which will stop the worksheet changed event from running when refreshed button is pressed - but it stops it from running at all (example of what I did)
Private Sub RefreshButton_Click()
Dim Flag as Boolean
Flag = True
Refreshbuttons
ActiveWorkbook.RefreshAll
Flag = False
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Flag = true then
Exit Sub
Else
[...] {Rest of code below here}
I am stuck - any help is greatly appreciated!!!
Thanks,
Ethan
EDIT
Thanks Tim! you pointed me in the right direction. I ended up going with (code below) and it worked beautifully. I appreciate everyones help!
Private Sub RefreshButton_Click()
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
Else
End If
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
MsgBox "Refresh Complete"
End Sub
Private Sub RefreshButton_Click()
On Error Goto haveError
Refreshbuttons
Application.EnableEvents = False
ActiveWorkbook.RefreshAll
haveError:
Application.EnableEvents = True
End Sub
Declare Flag as global like below. It should work.
Public Flag as Boolean
All office applications have a built-in function to control this behavior. Simply add:
Application.EnableEvents = False to the beginning of the RefreshButton_Click event and
Application.EnableEvents = True to the end of the event.
Is there an object and/or function that can detect or represent a general change on a worksheet? Something similar to (for example) ComboBox1_Change() but could be applied to a whole worksheet. Almost something like Worksheet1_Change(). Any suggestions are welcome. Thanks.
The simplest would be to use the worksheet change event:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "Change Detected!"
End Sub
There are instances will this will throw you in to an infinite loop. Consider this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Range("A1").Value = "Change"
End Sub
Set a breakpoint on the Range("A1") code and make a change somewhere else on the sheet and count how many time that breakpoint gets hit. When you get tired of hitting F5, stop the code and try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Range("A1").Value = "Change"
Application.EnableEvents = True
End Sub
You'll see that the code only fires once. Which is probably what you're after.
It's very important that you have the Application.EnableEvents = True line in there, or else you'll get the appearance that your code isn't working (by default EnableEvents does not revert back to True at the end of the code).