Load from a desktop

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
767
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I have written (borrowed and amended) some code to open a file off my desktop which works fine.
But as several people have access to the main file and can load a file from their desktop I am having issues making this work.

I have created a cell reference "desktop" that mirrors the location dependant upon who is using the file (amendments are made to the desktop based on username) so that it does look like
this is the location I would normally use Set src = Workbooks.Open("C:\Users\THIS CHANGES\Desktop\file.xlsx", but it fails in the code below saying the file cannot be found.

Whats the best way around this as I could make sure the file is saved in a specific location, but using the desktop is easier.

Code:
Sub GrabACWAHTData()

Dim lCopyLastRow As Long, lDestLastRow As Long
Dim desktop As String
    
desktop = Worksheets("Calcs").Range("A23").Value

    Application.ScreenUpdating = False
        Sheet2.Visible = True
    
    Set wsCopy = Workbooks("source.xlsm").Worksheets("sheet")
    Set wsDest = Workbooks.Open(desktop)
            wsDest.Parent.Windows(1).Visible = False
    
    lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "P").End(xlUp).Row
    
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

    wsCopy.Range("A2", wsCopy.Cells(lCopyLastRow, "A")).Copy
    wsDest.Cells(lDestLastRow, "A").PasteSpecial xlValues
        Sheet2.Visible = xlVeryHidden
            wsDest.Parent.Close True
                Sheets("Welcome").Select
                Range("E11").Select
                Range("E11").Value = Format(Now(), "mm/dd/yyyy hh:mm")

    Application.ScreenUpdating = True
    Application.CutCopyMode = False
End Sub
[code/]

thanks in advance
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You can use the Environ function to retrieve the user's name. For example...

VBA Code:
Set src = Workbooks.Open("C:\Users\" & Environ("username") & "\Desktop\file.xlsx")
 
Upvote 0
thank you @Domenic, but I think I have made a mess of this.
Can you confirm if I am thinking correctly at the moment. Using the VBA above the wsCopy is where I am copying the data from and the wsDest is where I should be copying it too?

If so I think I need to copy from the file on the desktop to my open current file and this code is doing it the opposite way round.
How hard is it to change this round?

thanks
 
Upvote 0
Sorry, but I'm confused, can you please restate your question?
 
Upvote 0
The VB I originally wrote was written I think using code to copy from an open file to a closed one on the desktop. My fault really trying to change someone elses and not writing completely from scratch.

I need to do it the other way round
Copy a range from the sheet on the desktop which is closed into my current open one.
 
Upvote 0
I thought you had an issue with the username to be specified in the path to be used in opening your workbook? Anyway, try...

VBA Code:
'open the workbook referenced by the variable 'desktop'
Dim wbCopy As Workbook
Set wbCopy = Workbooks.Open(desktop)

'get the Sheet1 from the opened workbook (change the sheet name accordingly)
Dim wsCopy As Worksheet
Set wsCopy = wbCopy.Worksheets("Sheet1")

'get the destination worksheet (change the workbook and sheet names accordingly)
Dim wsDest As Worksheet
Set wsDest = Workbooks("Book1.xlsm").Worksheets("Sheet1")

Then it looks like you'll need to amend other parts of your code accordingly.
 
Upvote 0
thank you for this @Domenic originally I did have an issue with the time, but realised I had written the original VBA incorrectly.
The time issue is sorted, I just need to amend the code, hopefully once I have done this I should be sorted
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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