New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Adapt code UserForm

Macros, VBA, Excel Automation, etc.

Adapt code UserForm

Postby marreco » Fri Jan 13, 2012 6:04 pm

Good night! People.

The code below works perfectly.
This code prevents the common change the User data.
As the sheet is used by two people (Master User, User common).

What I need is to adapt it so that when the Master User is changing some of the data, activate a UserForm.

Remembering aplanilha can not be blocked nor hidden, because two people operarm, and only one of them can change the data to another does not.

Could anyone help me?
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NewValue As Variant, OldValue As Variant
    If Target.Count > 1 Then Exit Sub
     'Say you want to work with the range A1:F100
    If Not Intersect(Target, Range("A1:F1000")) Is Nothing Then
        NewValue = Target.Value
        Application.EnableEvents = False
        Application.Undo
        OldValue = Target.Value
        If OldValue = "" Then
            Target.Value = NewValue
             'ElseIf 'Before there was InputBox Then
            Target.Value = NewValue
        Else: MsgBox "Você não pode alterar o conteudo da celula.", 16, "Células Bloqueadas"
            Target.Value = OldValue
        End If
        Application.EnableEvents = True
    End If
End Sub
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0

Re: Adapt code UserForm

Postby Sisyphus » Fri Jan 13, 2012 9:16 pm

Hi,
How about using the UserName property as an identifier?
Code: Select all
If Application.UserName <> "My Master's name" Then Exit Sub
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: Adapt code UserForm

Postby marreco » Sat Jan 14, 2012 3:08 pm

Good night.

Thanks for replying but I know almost nothing about VBA.

I put your code but it does not call the userform.

Can you tell me why?

Thanks
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0

Re: Adapt code UserForm

Postby Sisyphus » Sat Jan 14, 2012 3:27 pm

Hi,
You posted some code and I suggested an amendment to it, like this:
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NewValue As Variant, OldValue As Variant
    If Target.Count > 1 Then Exit Sub

    If Application.UserName <> "My Master's name" Then Exit Sub

     'Say you want to work with the range A1:F100
    If Not Intersect(Target, Range("A1:F1000")) Is Nothing Then
        NewValue = Target.Value
        Application.EnableEvents = False
        Application.Undo
        OldValue = Target.Value
        If OldValue = "" Then
            Target.Value = NewValue
             'ElseIf 'Before there was InputBox Then
            Target.Value = NewValue
        Else: MsgBox "Você não pode alterar o conteudo da celula.", 16, "Células Bloqueadas"
            Target.Value = OldValue
        End If
        Application.EnableEvents = True
    End If
End Sub

This extra line of code doesn't call any form. It prevents the InputBox (the form) from being shown if the user isn't the person you want to permit to do that. Of course, "My Master's name" isn't the application's UserName. To find the UserName you want go to Tools/Options and look at the bottom of the General tab. You must copy it very exactly. Another place to find the UserName is to type this in the Immediate Window of the VB Editor:
? Application.UserName

The name will appear below question you entered after you press Enter. You can copy it from there and paste it directly into the code.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: Adapt code UserForm

Postby marreco » Sat Jan 14, 2012 3:56 pm

Thank you!

One more question, my idea was not to allow the password to be seen and InputBox, not help.

Thank you!
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0

Re: Adapt code UserForm

Postby Sisyphus » Sun Jan 15, 2012 2:47 am

Hi,
It isn't the password you write in the code but the user name. Generally, the user name isn't a secret.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: Adapt code UserForm

Postby marreco » Sun Jan 15, 2012 5:14 am

Thanks....it's Solved! ;) .

Just wanted to say that my post was in the beginning because I need for a password.
But thank you!
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 249 guests