Macro if Specific Cell Value is Updated

hmltnangel

Active Member
Joined
Aug 25, 2010
Messages
290
Office Version
  1. 365
Platform
  1. Windows
Just being silly, but is this code right, or is there a better way of writing it? I want it to check if userid input in cell I2 meets the conditions, then run a macro if it does/msg box if it doesnt match. I only want it to run if i2 is changed.

VBA Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("I2")
If target.Value = Environ$("UserName") Then
 Call Macro1 'add macroname
ElseIf target.Value = "1234" Then
 Call Macro1 'add macroname
ElseIf target.Value = "2345" Then
 Call Macro1 'add macroname
ElseIf target.Value = "3456" Then
 Call Macro1 'add macroname
ElseIf target.Value = "4567" Then
 Call Macro1 'add macroname
ElseIf target.Value <> Environ$("UserName") Then
    MsgBox "UserID does not match Windows Login"
End If
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Edit, that code does work, but I did make a bit of a mess of it. I need to alter it so that the following happens:

If the UserID in I2 matches Environ Username, then it calls Macro1
If The Environ Username matches 1234,2345,3456,4567 then call Macro1
Otherwise - Msg box - Doesnt match.
 
Upvote 0
Actually Got it working :)
VBA Code:
Sub LineMan()
Set target = Range("I2")
If target.Value = Environ$("UserName") Then
 Call Autofiltercolumns   'add macroname
End If
If Environ$("UserName") = "1234" Then
 Call Autofiltercolumns   'add macroname
ElseIf Environ$("UserName") = "2345" Then
 Call Autofiltercolumns   'add macroname
ElseIf Environ$("UserName") = "3456" Then
 Call Autofiltercolumns   'add macroname
ElseIf Environ$("UserName") = "4567" Then
 Call Autofiltercolumns   'add macroname
End If
If target.Value <> Environ$("UserName") Then
    MsgBox "UserID does not match Windows Login"
End If
End Sub
 
Upvote 0
But I have one snag, any suggestions?

How to stop the msg box coming up if the call macro part runs ok? Basically I only want the msg box to pop up if the final part is true
 
Upvote 0
If you want it to run automatically, you need to keep it as a "Worksheet_Change" event procedure.

Also, a "Case" statement is much neater than a bunch of ElseIf statements.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address(0, 0) = "I2" Then
        If Target.Value = Environ$("UserName") Then
            Call Autofiltercolumns
        Else
            Select Case Target.Value
                Case "1234", "2345", "3456", "4567"
                    Call Autofiltercolumns
                Case Else
                    MsgBox "UserID does not match Windows Login"
            End Select
        End If
    End If
   
End Sub
 
Upvote 0
Thanks Joe, I need it to only run when a button is clicked. So I can assign the macro to a button really easy, but I dont want the auto run anymore. How do I amend that? I tried to change it and remove the target.address lines but it doesnt work
 
Upvote 0
Try this then:
VBA Code:
Sub LineMan()

    Dim userName As String
    
    userName = Range("I2").Value
    
    If userName = Environ$("UserName") Then
        Call Autofiltercolumns
    Else
        Select Case userName
            Case "1234", "2345", "3456", "4567"
                Call Autofiltercolumns
            Case Else
                MsgBox "UserID does not match Windows Login"
        End Select
    End If
   
End Sub
 
Upvote 0
Still got an error: Even with one of the userids selected it just pops up the msg box.

Basically, all I want is for it to

Carry out the Autofilter if the username matches the windows username or if the username matches one of the four additional usernames. But if the username doesnt match the windows username or the four additional ones, dont run the autofilter, and pop up a msg box.
 
Upvote 0
Still got an error: Even with one of the userids selected it just pops up the msg box.

Basically, all I want is for it to

Carry out the Autofilter if the username matches the windows username or if the username matches one of the four additional usernames. But if the username doesnt match the windows username or the four additional ones, dont run the autofilter, and pop up a msg box.
When you say you "still got an error", are you actually getting an error, or just unexpected results?
If an error, please provide the exact error code and message.

If it is not producing the expected results, run this code and tell me exactly what the first message box returns:
VBA Code:
Sub LineMan()

    Dim userName As String
    
    MsgBox "I2 Value: " & Range("I2").Value & vbCrLf & _
            "UserName: " & Environ$("UserName")
    
    userName = Range("I2").Value
    
    If userName = Environ$("UserName") Then
        Call Autofiltercolumns
    Else
        Select Case userName
            Case "1234", "2345", "3456", "4567"
                Call Autofiltercolumns
            Case Else
                MsgBox "UserID does not match Windows Login"
        End Select
    End If
   
End Sub
 
Upvote 0
Apologies Joe, I should be way more specific.

It doesnt produce the expected results - not an "error" as such. The code you just provided gives a msg box as follows: (note - names actually appear instead of numbers as im just using a number value for the names on here)

I2 Value: 0123
Username: 1234
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top