VBA - Combining a Sub and a Function

Nordicrx8

Board Regular
Joined
Dec 10, 2015
Messages
143
Office Version
  1. 365
Platform
  1. Windows
Hi all!

The below code works perfectly, but now I'm also trying to capture the username (To the left of the data, in column A) of the person who pressed the save button.

VBA Code:
Sub SaveBulk()

Application.EnableEvents = False
    Application.ScreenUpdating = False

    With Sheets("Output_Bulk")

        Dim FirstEmptyRow As Long        
        FirstEmptyRow = .Cells(.Rows.Count, "D").End(xlUp).Offset(1, 0).Row

        Sheets("Main").Range(Sheets("Admin").Range("G5") & ":" & Sheets("Admin").Range("G6")).Copy
        .Cells(FirstEmptyRow, "D").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True            
        Excel.Application.CutCopyMode = False
    End With
 
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    MsgBox "Bulk Saved"
    
End Sub

I found this code and it works well - I can enter "GetName(1)" in a cell, and it populates my name. But I'm having a hard time altering the code above to populate the users name in the same range as above, in column A. Tried a handful of different ways, but I think I may be taking the wrong approach.

Any help combining these would be appreciated! Thanks so much!

VBA Code:
Option Explicit

Function GetName(Optional NameType As String) As String
Application.Volatile
If Len(NameType) = 0 Then NameType = "OFFICE"
Select Case UCase(NameType)
          Case Is = "OFFICE", "1"
               GetName = Application.Username
               Exit Function
          Case Is = "WINDOWS", "2"
               GetName = Environ("UserName")
               Exit Function
          Case Is = "COMPUTER", "3"
               GetName = Environ("ComputerName")
               Exit Function
          Case Else
               GetName = CVErr(xlErrValue)
     End Select

End Function
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I don't understand the relationship between the name of the person who clicks a button and the name in a cell. How can you be sure they are the same? In any case, that could be an offset but no idea from where. Wouldn't Environ("Username") be more useful?
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,063
Members
449,090
Latest member
fragment

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