Get computer name

C

Chip Pearson

Eric,

Try something like

Debug.Print Environ("Computername")



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
K

Kevin

I use this in VBA within Access. It should also work in
Excel because it is using the Windows API

Function GetComputerName() As String
Dim lngLen As Long, lngX As Long
Dim strName As String

lngLen = 32
strName = String$(lngLen, 0)
lngX = apiGetComputerName(strName, lngLen)
If lngX <> 0 Then
GetComputerName = Left$(strName, lngLen)
Else
GetComputerName = ""
End If

End Function

You also need the following in the declarations for the
module where the function exists.

Public Declare Function apiGetComputerName Lib "kernel32"
Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As
Long) As Long

Hope that helps!

Kevin
 
K

Kevin

P.S. I have tested the code I just sent you in Excel and
it does work. I inserted the code into a Module for the
Workbook. I put the function header in the declarations. I
then called the function from a subroutine I run at
startup and inserted the value into a cell in my
spreadsheet and it works just fine.

Hope that helps!

Kevin
 
Top