how to edit this macro to export to desktop regardless of user?

qplsn9

New Member
Joined
Dec 5, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am using a VBA macro button to export a range of cells as a CSV file to the desktop. This file will be downloaded on multiple computers so I want the macro to save to the desktop regardless of which computer it is downloaded on.

VBA Code:
Sub seetest()

    Const CSVDATA = "B1:IG2"
    Dim ws As Worksheet, filename As String
    
    filename = "C:\Users\" & Environ("Username") & _
               "\Desktop\Labels " & Format(Date, "YYYY-MM-DD") & ".csv"
    
    Set ws = ThisWorkbook.ActiveSheet
    With Workbooks.Add(1)
        ws.Range(CSVDATA).Copy .Sheets(1).Range("A1")
        .SaveAs filename, xlCSV
        .Close
    End With
       
    MsgBox CSVDATA & " exported to " & filename, vbInformation

End Sub

This is the code for my macro and when I do it on my laptop, the computer that I created the files on, it works; however, when any of my coworkers do it the CSV file isn't auto saved to the desktop but instead it is opened with Excel and these errors populate. I could just change the directions so that instead of just pressing the export button that they press it, save the file themselves, and then use it but I want to automate this as much as possible. Any ideas why I receive these errors on other laptops but not mine?

enter image description here


enter image description here


Someone suggested using
VBA Code:
"C:\Users\" & Environ("Username") & "\Desktop\ "? Does Environ("Username")


But I am unsure of how to incorporate that into my current code.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Perhaps their desktops are mapped somewhere else. Try using:

VBA Code:
filename =  createobject("wscript.shell").specialfolders("desktop") & "\Labels " & Format(Date, "YYYY-MM-DD") & ".csv"
 
Upvote 0
That usually pops up when an extension is specified, but not a Format. Syntax should be
VBA Code:
ThisWorkbook.SaveAs FileName:="C:\Users\UserName\Folder\FileName.csv", FileFormat:="xlCSV"
 
Upvote 0
Another reason might be OneDrive being the destination,
if a person has Onedrive backup enabled, this means their "Desktop" iss on the onedrive, not on the machine.

if you disable the onedrive backup - it should work fine.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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