Quick question about MAC address

WarPigl3t

Well-known Member
Joined
May 25, 2014
Messages
1,611
Hey everyone. Quick question. I have the below function which will return the MAC address of the computer being used. I was just running some tests when I realized my MAC address was "" or blank in other words. I thought about it and realized that my internet went down and I disconnected from the network while I was working on my excel project. So then I thought, could not being connected to the internet be affecting the macro from returning my MAC address? So I connected back to the internet to test my theory. Then when I ran my macro, it did return my MAC address. So my question is, why is my MAC address being returned by the below function only when I am connected to the internet? A MAC address is like the address of your house. It doesn't change and being connected to the internet has nothing to do with a MAC address. So why is my function returning blank when I'm not connected to the internet?

Code:
Function GetMACAddress() As String
    Dim objVMI As Object
    Dim vAdptr As Variant
    Dim objAdptr As Object
    Dim adptrCnt As Long
    Dim adres As String
    adres = ""
    Set objVMI = GetObject("winmgmts:\\" & "." & "\root\cimv2")
    Set vAdptr = objVMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
    For Each objAdptr In vAdptr
        If Not IsNull(objAdptr.MACAddress) And IsArray(objAdptr.IPAddress) Then
            For adptrCnt = 0 To UBound(objAdptr.IPAddress)
                If Not objAdptr.IPAddress(adptrCnt) = "0.0.0.0" Then
                    GetNetworkConnectionMACAddress = objAdptr.MACAddress
                    Exit For
                End If
            Next adptrCnt
            [COLOR=#008000] 'MsgBox "Your MAC Address is: " & GetNetworkConnectionMACAddress[/COLOR]
            adres = GetNetworkConnectionMACAddress
        End If
    Next
    GetMACAddress = adres
End Function
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Thanks mole. That answered my question. So I can't get the MAC address without being connected to a network. I'm going to have to modify my other code to say, if the returned MAC address is "" Then ....
 
Upvote 0
store it on first connection on an xlveryhidden sheet in white text on first use, then if its not found in session, use the hidden value to allow the rest of your code to work
 
Upvote 0
This bit:
Code:
WHERE IPEnabled = True
basically excludes any adapters that aren't connected.
 
Upvote 0
mole: I already do that but with a textbox that is hidden and protected from modification by user. My code pretty much says, if the textbox is blank, get MAC address and put it in textbox. If the MAC address is not blank, then make sure the MAC address in the textbox matches the MAC address of the computer using the workbook. This prevents people from dispersing the workbook to other computers.

RoryA: That line of code would pretty much do the same thing as writing an if statement that says if MAC address returned from function is blank than exit sub. But I do like the idea and thank you for teaching me something new.
 
Upvote 0
I think you missed my point. That is already in your code and it is what causes you to get no MAC address returned when you have no network connection.
 
Upvote 0
You'll have to forgive me, but Im not entirely sure what your code does. That's why I missed the point. My guess is that your code pretty much says, if the computer is connected to the internet then run the rest of my code. I have another theory that your code translates to, enable the NIC card on my computer so that the MAC address will get returned rather than just returning blank when not connected to a network.
 
Upvote 0
I just reviewed my code and saw where it says Where IPEnabled = True. I'll try removing that bit of code and see if it fixes the problem. I'll do it tomorrow though. Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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