VBA - link to [any] the current user's desktop

AV_Geek

New Member
Joined
Jan 23, 2022
Messages
32
Office Version
  1. 365
Platform
  1. MacOS
I'm trying to build a Mac that will link to the desktop of the current user, whoever that is.

Using these two macros:

VBA Code:
Sub RunOnAllFilesInFolderWindows()

Dim wbOpen As Workbook

Dim MyDir As String

MyDir = "C:\Users\00000000\Desktop\Action" 'This is the path to your files



'Comment out the 3 lines below to debug'Application.ScreenUpdating = False'Application.Calculation = xlCalculationManual'On Error Resume Next

strExtension = Dir(MyDir & "\*.xlsx")





While strExtension <> vbNullString

Set wbOpen = Workbooks.Open(MyDir & "\" & strExtension)



With wbOpen

Call RemoveUnnecessary

Call RemoveXpoints

Call Rename_Sheet

ActiveWorkbook.SaveAs fileName:="C:\Users\00000000\Desktop\Action\" & ActiveSheet.Name & ".xlsx"



.Close SaveChanges:=True

End With



strExtension = Dir

Wend



Application.ScreenUpdating = True

Application.Calculation = xlCalculationAutomatic

End Sub



Sub CombineFilesWindows()



Dim Path As String

Dim fileName As String

Dim Wkb As Workbook

Dim ws As Worksheet



Application.EnableEvents = False

Application.ScreenUpdating = False

Path = "C:\Users\00000000\Desktop\Action\" 'Change as needed

fileName = Dir(Path & "\*.xls", vbNormal)

Do Until fileName = ""

Set Wkb = Workbooks.Open(fileName:=Path & "\" & fileName)

For Each ws In Wkb.Worksheets

ws.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

Next ws

Wkb.Close False

fileName = Dir()

Loop

Application.EnableEvents = True

Application.ScreenUpdating = True



End Sub



If user 00000000 is logged in, this works. I need something for user 00000000,00000001, 00000002, 00000003, etc.

I tried the following, but none worked:

VBA Code:
C:\Users\*\Desktop\Action
C:\Users\userprofile\Desktop\Action
C:\Users\*%userprofile%\Desktop\Action
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Userprofile includes the Drive and Folder, try for the 2nd macro (same syntax for the first macro)....
VBA Code:
ActiveWorkbook.SaveAs Filename:=Environ("userprofile") & "\Desktop\Action\" & ActiveSheet.Name & ".xlsx"
or
VBA Code:
Dim strFolderName As String
strFolderName = CreateObject("wscript.shell").specialfolders("Desktop") & "\Action\" & ActiveSheet.Name & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFolderName
 
Upvote 0
Userprofile includes the Drive and Folder, try for the 2nd macro (same syntax for the first macro)....
VBA Code:
ActiveWorkbook.SaveAs Filename:=Environ("userprofile") & "\Desktop\Action\" & ActiveSheet.Name & ".xlsx"
or
VBA Code:
Dim strFolderName As String
strFolderName = CreateObject("wscript.shell").specialfolders("Desktop") & "\Action\" & ActiveSheet.Name & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFolderName
I apologize, but I don't understand what I should do.

try for the 2nd macro (same syntax for the first macro)....

Are you saying I should use either of the above corrections in my second macro, but leave my first macro alone, with the user name in the path???

Also, if I need to use the second mod that you made, what does it replace?
 
Upvote 0
They both replace
VBA Code:
ActiveWorkbook.SaveAs fileName:="C:\Users\00000000\Desktop\Action\" & ActiveSheet.Name & ".xlsx"
In the 2nd macro

You use
VBA Code:
Environ("userprofile") & "\Desktop\Action\" & ActiveSheet.Name
anywhere you need the path to the file on the desktop
for instance
VBA Code:
MyDir = "C:\Users\00000000\Desktop\Action"
becomes
VBA Code:
MyDir = Environ("userprofile") & "\Desktop\Action\" & ActiveSheet.Name

With the specialfolders you put
VBA Code:
Dim strFolderName As String
strFolderName = CreateObject("wscript.shell").specialfolders("Desktop") & "\Action\" & ActiveSheet.Name


Somewhere near the top of your code then use strFolderName as your file path wherever you need it i.e.

VBA Code:
MyDir = "C:\Users\00000000\Desktop\Action"

becomes

VBA Code:
MyDir = strFolderName

Please note that I have removed the .xlsx so it can be used generically in the code so when you need the extension you need to add it back in i.e,

VBA Code:
ActiveWorkbook.SaveAs Filename:=strFolderName & ".xlsx"
 
Upvote 0
Solution
I think I got it. I just need to test it on another user. Thanks!!!!!!
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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