ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Assign cell value from VBA function (https://www.excelbanter.com/excel-programming/308760-assign-cell-value-vba-function.html)

easy_forshezee

Assign cell value from VBA function
 
Hello,

Please help out a newbie. How can I assign a cell value from a VBA
function? I have the following in my VBA code:
Private Sub Workbook_Open()
MsgBox username()
End Sub


' Return the user's name.
Public Function username() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long

user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
username = "<unknown"
Else
username = Left$(user_name, name_len - 1)
End If
End Function


This works fine for displaying the Msgbox, but I'd like to put the
username in a specific spreadsheet cell.

Any help is appreciated.
Eric

mangesh_yadav[_78_]

Assign cell value from VBA function
 
instead of
MsgBox username()
put
ActiveSheet.Range("A1") = username()


- Manges

--
Message posted from http://www.ExcelForum.com


Nikos Yannacopoulos[_5_]

Assign cell value from VBA function
 
Eric,

Just type:

=username()

in the cell.

HTH,
Nikos

"easy_forshezee" wrote in message
om...
Hello,

Please help out a newbie. How can I assign a cell value from a VBA
function? I have the following in my VBA code:
Private Sub Workbook_Open()
MsgBox username()
End Sub


' Return the user's name.
Public Function username() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long

user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
username = "<unknown"
Else
username = Left$(user_name, name_len - 1)
End If
End Function


This works fine for displaying the Msgbox, but I'd like to put the
username in a specific spreadsheet cell.

Any help is appreciated.
Eric




Nikos Yannacopoulos[_5_]

Assign cell value from VBA function
 
Eric,

Just type:

=username()

in the cell.

HTH,
Nikos

"easy_forshezee" wrote in message
om...
Hello,

Please help out a newbie. How can I assign a cell value from a VBA
function? I have the following in my VBA code:
Private Sub Workbook_Open()
MsgBox username()
End Sub


' Return the user's name.
Public Function username() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long

user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
username = "<unknown"
Else
username = Left$(user_name, name_len - 1)
End If
End Function


This works fine for displaying the Msgbox, but I'd like to put the
username in a specific spreadsheet cell.

Any help is appreciated.
Eric





All times are GMT +1. The time now is 10:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com