VB Code that reads computer log on

PritiOne

Active Member
Joined
Jun 2, 2004
Messages
357
I am trying to write a program that runs through Excel - using VB Code to read the computer user logon. Can anyone help me?

Thanks!
 
I don't see anything wrong with what you've got. Looks like it'll work fine to me. What does it do/not do for you? (ie, what error are you getting and what's being highlighted?)

Dan
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
On:

Sheets("Sheet1").Range("A1").Value = Environ("UserName")

I get Application defined or object defined error.
 
Upvote 0
Also - I need to get the first digit of one cell a2
and first 7 digits of another cell a2 in the same code... HELP???
 
Upvote 0
Possibly something along these lines is what you're after:

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_Open()
    <SPAN style="color:#00007F">Dim</SPAN> strUser <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> c
    <SPAN style="color:#00007F">Dim</SPAN> FirstAddress
        strUser = Environ("USERNAME")
        
    Sheets("Sheet1").Range("A1").Value = strUser
    
    <SPAN style="color:#007F00">'   Assuming that you list of users is on a sheet called "Authorized Users"</SPAN>
    <SPAN style="color:#007F00">'   Test if the Current User is Authorized to use the workbook</SPAN>
    <SPAN style="color:#00007F">With</SPAN> Sheets("Authorized Users").Range("A1:A166")
        <SPAN style="color:#00007F">Set</SPAN> c = .Find(strUser, LookIn:=xlValues)
        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
            FirstAddress = c.Address
            <SPAN style="color:#00007F">Do</SPAN>
                MsgBox strUser & " is authorized for use", vbInformation + vbOKOnly, "Authorized Use"
                <SPAN style="color:#00007F">Set</SPAN> c = .FindNext(c)
            <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> And c.Address <> FirstAddress
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
            MsgBox strUser & " is not authorized for use", vbCritical + vbOKOnly, "Unauthorized Use"
            <SPAN style="color:#007F00">'   ActiveWorkbook.Close False</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Smitty
 
Upvote 0
I need to get the first digit of one cell a2
and first 7 digits of another cell a2 in the same code...

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> Digits()
    <SPAN style="color:#00007F">Dim</SPAN> x <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
        x = Sheets("Sheet1").Range("A2")
        y = Sheets("Sheet2").Range("A2")
        
    MsgBox Left(x, 2) & " - " & Left(y, 7)
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Smitty
 
Upvote 0

Forum statistics

Threads
1,216,179
Messages
6,129,334
Members
449,503
Latest member
glennfandango

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