I have 2 listboxes. If either of the listboxes are selected I wouldl ike a message box pop up when the unselected listbox is trying to be selected saying something.
for example:
if e2>0 and e3>0 then
display messagebox "alsdkjfaslj"
end if
e2 and e3 are cell links from the listboxes.
I can't think of a good way to go about this and am new to vba. Please help this newbie out.
Thanks
You can listen to your worksheet change event for the specific cells you want to trigger your event like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If Application.ActiveSheet.Cells(3, 5).Address = Target.Address Then
MsgBox ("you clicked E3")
ElseIf Application.ActiveSheet.Cells(2, 5).Address = Target.Address Then
MsgBox ("you clicked E2")
End If
Application.EnableEvents = True
End Sub
Make sure the code is in the sheet that you want to watch:
Related
I want a MsgBox to appear when cell A1 is clicked. I want it to appear even if A1 is already active when it is clicked:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If Target.Row = 1 And Target.Column = 1 Then
MsgBox ("message")
End If
Application.EnableEvents = True
End Sub
This code works only if cell A1 is not already selected when I click on it. Currently the message box does not appear in this case.
Is there a way to fix this?
Your code is using Worksheet_SelectionChange which only fires when a different cell is selected (hence the name Selection Change).
Alternatively, if it's okay if your [unknown] goal is attained using double click or right click then there are other worksheet events that will help:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox Target.Address & " was double clicked"
Cancel = True 'don't edit cell
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
MsgBox Target.Address & " was right clicked"
Cancel = True 'don't open context menu
End Sub
Note that the code for these event procedures need to be placed in the worksheet module.
Edit: More creative ways
Click Event via PeekMessage API
If it must be a single click, there are "sneakier" ways to accomplish this, such as adding a Click event. This is not a built-in feature of Excel VBA, and thus, this method is not generally recommended.
It involves checking for the WM_MOUSEMOVE message when a cell is mouse-clicked, which is accomplished by calling the PeekMessage API inside the Worksheet_SelectionChange event. More info and examples here.
Transparent command button
There could also be a round-about way to accomplish this using an ActiveX Command Button with no caption, with the BackStyle property set to frmBackStyleTransparent.
Neither of these methods have been tested and you might need to do some fancy coding to get them to work. Depending on how often the same cell will be clicked that is already selected (and therefore how that functionality is to you), you may want to simply re-think the layout of your worksheet.
For example, you could add an extra column and have the user click the cell next to the one with the value to activate your message box.
More Information:
MSDN : Worksheet.BeforeDoubleClick Event (Excel)
MSDN : Worksheet.BeforeRightClick Event (Excel)
Chip Pearson : Events in Excel VBA
I read a lot of pages saying that, but none of them put the solution if the value change by an "if function" not by hand.
The code I get is that:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A18:A30")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
It only works if I change the value by hand.
Thank you in advance.
Another solution; instead of triggering your function every time when your worksheet recalculates, add a function in a module:
Function DetectChange() As Integer
MsgBox "You changed THE CELL!"
DetectChange = 0
End Function
Assuming the outcome of your formula is numeric:(otherwise outcome of function must be a empty string and the "+" must be "&")
Add to your IF-formula at the end ...+Detectchange()
Now there will be a msgbox only when your formula is recalculated
Edit by Darren Bartrup-Cook:
I found this code gave worked when the formula recalculated. It didn't fire if I changed a cell that doesn't affect the cell it's entered to and it didn't fire using Calculate Now or Calculate Sheet.
It did occasionally fire for all formula that I used the function in, but that seemed to be when I was debugging - maybe further investigation needed.
Public Function DetectChange()
MsgBox "You changed cell " & Application.Caller.Address
End Function
e.g.:
=IF(A1=1,A2,A3) & DetectChange() entered in cell A4 displays the message "You changed cell $A$4" if cells A1, A2 or A3 is changed.
Write this in Sheet1 and run the TestMe sub:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1:A30")) Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error GoTo Finalize
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
Sub TestMe()
Range("A1") = 34
End Sub
It has worked quite ok on my PC.
If the cell is changed by a built-in Excel function, then the comment of #Vincent G states the correct answer:
Worksheet_Change event occurs when cells on the worksheet are changed by the user or by an external link. and This event does not occur when cells change during a recalculation. Use the Calculate event to trap a sheet recalculation.
If you want to track the calclulation event based on some changes at Range(A18:A30) this is a working solution:
Add a new Worksheet to your Workbook (Sheet2);
In the current Worksheet write the Calculate event:
Private Sub Worksheet_Calculate()
Dim cell As Range
For Each cell In Sheet2.Range("A18:A30")
If cell <> Sheet1.Range(cell.Address) Then
cell = Sheet1.Range(cell.Address)
End If
Next cell
End Sub
In the Sheet2 write an event, catching the changes.
As simple as #Vincent G says.
Private Sub Worksheet_Calculate()
Call YourFunction
End Sub
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
Its a protected worksheet/workbook and I have a code that will throw a prompt for the user, whether to edit the sheet or not. Cells are editable, but the problem is cells are not getting highlighted with border. So its difficult for the user to know which cells is he working on.
I have 2 sheets here, Corefiller and Ad-filler, if dropdown on corefiller sheet is "No". User gets a prompt when he selects the sheet, he clicks ok to edit the sheet or cancel if he doesnt want to edit.
Code on Sheet "Ad-filler"
Option Explicit
Private mMessageDisplayed As Boolean
Private Sub Worksheet_Activate()
Carry
End Sub
Code on a module.
Public Sub Carry()
If ActiveSheet.ProtectContents And Not mMessageDisplayed Then
mMessageDisplayed = True
If ThisWorkbook.Sheets("Corefiller").Range("E29") = "NO" Then
If MsgBox("Click OK to include Filler for this request", vbOKCancel + vbInformation) = vbOK Then
ThisWorkbook.Worksheets("Corefiller").Range("E29") = "YES"
With ThisWorkbook.Sheets("Ad-filler")
.Range("E13:E14").Locked = False
End With
Else
With ThisWorkbook.Sheets("Ad-filler")
.Range("E13:E14").Locked = True
End With
End If
Else
Exit Sub
End If
End If
End Sub
Whats wrong in my code? why the cell is not highlighted. If i try to use protect/unprotect in the code, cells on the first sheet (Corefiller) will not be highlighted and I have to click on other sheets and come back to get the cell highlighted.
Can you restart, implement this and check whether the problem still exists:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = 0
Target.Interior.ColorIndex = 3
End Sub
Need your help on modifying the code or code that will help me do complete my task.
Its a protected workbook with VBA. I have a drop down on sheet1 in B18,D20,K11 and M46 and list of values in each drop down's.
When user changes the values in drop-down B18 and D20, I want to throw a prompt with "ok" and "cancel". User selects "ok" it should run a Module 1 ( which renames sheet).
Two things I am failing to achieve.
1. I don't want to throw prompt if user changes any other drop down values apart from 2 cells mentioned above,.
2. When clicked "OK" it should run module 1.
Please suggest if you have better code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNg As Range
Set rNg = Range("B18", "D20")
MsgBox "Please click Calculate Button", vbOKOnly, "Calculate button"
End Sub
Try the code below.
(don't understand why you want to check if the user pressed "OK" since you are using a MsgBox with vbOKOnly and he has no other option)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNg As Range
Dim IntersectRange As Range
Dim message As Integer
' modify here to the Range you need monitored
Set rNg = Range("B18", "D20")
Set IntersectRange = Intersect(Target, rNg)
If Not IntersectRange Is Nothing Then
message = MsgBox("Please click Calculate Button", vbOKOnly, "Calculate button")
If message = vbOK Then
Call Module1 ' or your Module / Function name
End If
Else
'Do Nothing if outside range
End If
End Sub
I am not saying this is the wright method, but you could use a UserForm to create custom prompts. And this is the way i do it, because for me it gives a better control on what i need to do.
Suppose if you create a userform with ok and cancel button, then you trigger the userform on the dropdown value change,
Sub DropDown1_Change()
UserForm1.Show
End Sub
Then write the codes for ok and cancel button clicks
Private Sub Ok_Click()
UserForm1.Hide
MsgBox "clicked ok"
End Sub
Private Sub cancel_Click()
UserForm1.Hide
MsgBox "clicked cancel"
End Sub