vba 如何在单元格中显示当前用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6934169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:43:33  来源:igfitidea点击:

How to show current user name in a cell?

excelvbaexcel-2003worksheet-function

提问by Anthony Kong

In most of the online resource I can find usually show me how to retrieve this information in VBA. Is there any direct way to get this information in a cell?

在我可以找到的大多数在线资源中,通常会向我展示如何在 VBA 中检索这些信息。有没有直接的方法可以在单元格中获取这些信息?

For example as simple as =ENVIRON('User')(which did not work)

例如像这样简单=ENVIRON('User')(不起作用)

回答by Jon Egerton

Based on the instructions at the link below, do the following.

根据以下链接中的说明,执行以下操作。

In VBA insert a new module and paste in this code:

在 VBA 中插入一个新模块并粘贴以下代码:

Public Function UserName()
    UserName = Environ$("UserName")
End Function

Call the function using the formula:

使用以下公式调用函数:

=Username()

Based on instructions at:

根据以下说明:

https://support.office.com/en-us/article/Create-Custom-Functions-in-Excel-2007-2f06c10b-3622-40d6-a1b2-b6748ae8231f

https://support.office.com/en-us/article/Create-Custom-Functions-in-Excel-2007-2f06c10b-3622-40d6-a1b2-b6748ae8231f

回答by Rafa Barragan

if you don't want to create a UDFin VBAor you can't, this could be an alternative.

如果您不想在VBA 中创建UDF,或者您不能,这可能是另一种选择。

=Cell("Filename",A1)this will give you the full file name, and from this you could get the user name with something like this:

=Cell("Filename",A1)这将为您提供完整的文件名,您可以从中获取用户名,如下所示:

=Mid(A1,Find("\",A1,4)+1;Find("\";A1;Find("\";A1;4))-2)

=Mid(A1,Find("\",A1,4)+1;Find("\";A1;Find("\";A1;4))-2)



This Formula runs only from a workbook saved earlier.

此公式仅从之前保存的工作簿运行。

You must start from 4th position because of the first slash from the drive.

由于驱动器的第一个斜线,您必须从第 4 个位置开始。

回答by web solusi

Example: to view the Windows User Name on Cell C5, you can use this script :

示例:要查看 Cell C5 上的 Windows 用户名,您可以使用以下脚本:

Range("C5").Value = ": " & Environ("USERNAME").

回答by rolacher

This displays the name of the current user:

这将显示当前用户的名称:

Function Username() As String
    Username = Application.Username
End Function

The property Application.Usernameholds the name entered with the installation of MS Office.

该属性Application.Username包含在安装 MS Office 时输入的名称。

Enter this formula in a cell:

在单元格中输入此公式:

=Username()

回答by sangorys

Without VBA macro, you can use this tips to get the username from the path :

如果没有 VBA 宏,您可以使用此提示从路径中获取用户名:

=MID(INFO("DIRECTORY"),10,LEN(INFO("DIRECTORY"))-LEN(MID(INFO("DIRECTORY"),FIND("\",INFO("DIRECTORY"),10),1000))-LEN("C:\Users\"))

回答by Lance Roberts

The simplest way is to create a VBA macro that wraps that function, like so:

最简单的方法是创建一个包装该函数的 VBA 宏,如下所示:

Function UserNameWindows() As String
    UserName = Environ("USERNAME")
End Function

Then call it from the cell:

然后从单元格中调用它:

=UserNameWindows()

See this articlefor more details, and other ways.

有关更多详细信息和其他方式,请参阅本文