VBA to check computer name and enable macro.

aliaslamy2k

Active Member
Joined
Sep 15, 2009
Messages
416
Office Version
  1. 2019
Platform
  1. Windows
Dear Colleagues,


I dont know if it is possible.. I have prepared a workbook, with many macros in it, the problem is anybody can copy my workbook and paste in their system. I need a vba code which should recognise my computer name and enable the macro or else all macros should be disabled.


Computer Name : AB



Many thanks in advance Mr Excel.

Kind regards,
AB
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It would be easier to check your Windows logon name

Code:
If Environ("username") <> "VoG" Then Exit Sub
 
Upvote 0
Hi VoG,

Thanks a million, would it be possible for you to explain where exactly i should enter this code, and how the response will come.


Many thanks
AB
 
Upvote 0
You could use it in a sub like this

Code:
Sub ArryTest()
Dim Myarray
Dim i As Long, j As Long
If Environ("username") <> "VoG" Then Exit Sub
Myarray = Range("A1:B10")
For i = 1 To 10
    For j = 1 To 2
        Cells(i, j).Offset(, 2).Value = Myarray(i, j)
    Next j
Next i
End Sub

Or possibly in the workbook open event in the ThisWorkbook module to close the workbook

Code:
Private Sub Workbook_Open()
If Environ("username") <> "VoG" Then Me.Close False
End Sub
 
Upvote 0
Hi VoG,


You are tooooo good.... Many thanks, you made my day.


Thank you very much Mr Excel.


Kind regards,
AB:)
 
Upvote 0
If you did want to lock access to a specific computer rather than user-id then you can use the following to return the local machine

Cheers

Dave

Code:
Sub ComputerName()
Dim objWsh
Set objWsh = CreateObject("WScript.Network")
MsgBox "Local computer name is " & objWsh.ComputerName
End Sub
 
Upvote 0
Hi Brettdj,

This is great. Do you know if a user change/modify their computer info somehow to match another computer's ID?

Many thanks,
Snelly

If you did want to lock access to a specific computer rather than user-id then you can use the following to return the local machine

Cheers

Dave

Code:
Sub ComputerName()
Dim objWsh
Set objWsh = CreateObject("WScript.Network")
MsgBox "Local computer name is " & objWsh.ComputerName
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,015
Members
449,060
Latest member
LinusJE

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