Help to open file when match HDD serialnumber

nhnn1986

Board Regular
Joined
Oct 12, 2017
Messages
92
Hi all

I have excel file that a little computer can open it, according to HDD serialnumber (C:\)

Code belove can't run, please fix it. I'm not good at VBA code

Thanks./.
Code:
Private Sub Workbook_Open()Dim oFSO As Object, drive As Variant, driveC As Variant
    Set oFSO = CreateObject("Scripting.FileSystemObject").GetDrive("C:\")
    Set drive.Value = oFSO.serialnumber.Value
With Sheets("Username")
    driveC = .Range("A1").Value
End With
If Not Application.Match(driveC, drive, 0) Then
    MsgBox "Hallo " & Environ("UserName")
    Application.Quit
Else
    MsgBox "Wellcome " & Environ("UserName")
End If
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,
Try this:
Rich (BB code):
Private Sub Workbook_Open()
  Dim SerialNumber As String
  SerialNumber = CreateObject("Scripting.FileSystemObject").Drives("C").SerialNumber
  If SerialNumber = Me.Sheets("Username").Range("A1").Value Then
    MsgBox "Wellcome " & Environ("UserName")
  Else
    MsgBox "Hello " & Environ("UserName") & vbLf _
         & "Sorry, but your can't use this workbook!", _
            vbExclamation, "Close"
    Me.Close False
  End If
End Sub
 
Last edited:
Upvote 0
Thanks @ZVI, ode work but when I run code the serial is nagtive like:[TABLE="width: 135"]
<tbody>[TR]
[TD="width: 135, align: right"]-1090195401[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
You are welcome! Negative serial number is native, but you may use its absolute value as well:
SerialNumber =Abs(CreateObject("Scripting.FileSystemObject").Drives("C").SerialNumber)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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