Results 1 to 5 of 5

Thread: [RESOLVED] Determine which computer is running Excel file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Resolved [RESOLVED] Determine which computer is running Excel file

    I have model in Excel that are run from two different computers (a desktop and a laptop). The model uses a file to import data that is located in a OneDrive folder. The path to that OneDrive folder differs on the two computers. I then have a Const that is set to the OneDrive folder so the model know where to find the file for the import.

    Is there a way in VBA I can detect, at startup, which computer is running the model and then set the Const programmatically to the correct folder? Something like:

    Code:
    If 'Computer is Desktop' then
         Const FS_Intermediate_File as String = 'path to OneDrive folder on desktop'
    ElseIf 'Computer is Laptop' then
         Const FS_Intermediate_File as String = 'path to OneDrive folder on laptop'
    End If
    Currently I change the Const declaration manually

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,979

    Re: Determine which computer is running Excel file

    Don't put in a Const but in a Public variable.

  3. #3
    Addicted Member
    Join Date
    Jul 2022
    Posts
    243

    Re: Determine which computer is running Excel file

    You could use the Environ function to look at the computer name https://vbaf1.com/variables/environment/
    Code:
    If Environ("COMPUTERNAME") = "[your desktop computers name]" then
         FS_Intermediate_File as String = 'path to OneDrive folder on desktop'
    ElseIf Environ("COMPUTERNAME") = "[your laptop computers name]" then
         FS_Intermediate_File as String = 'path to OneDrive folder on laptop'
    End If

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Re: Determine which computer is running Excel file

    Quote Originally Posted by jdelano View Post
    You could use the Environ function to look at the computer name https://vbaf1.com/variables/environment/
    Code:
    If Environ("COMPUTERNAME") = "[your desktop computers name]" then
         FS_Intermediate_File as String = 'path to OneDrive folder on desktop'
    ElseIf Environ("COMPUTERNAME") = "[your laptop computers name]" then
         FS_Intermediate_File as String = 'path to OneDrive folder on laptop'
    End If
    Where would the best place be to put this code so that it executes on opening the workbook? Don't know what executes when

    Not too worry. Found Workbook_Open
    Last edited by Bezzie; Mar 7th, 2024 at 05:44 AM.

  5. #5
    Addicted Member
    Join Date
    Jul 2022
    Posts
    243

    Re: [RESOLVED] Determine which computer is running Excel file

    There you go

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width