Consulting

Results 1 to 7 of 7

Thread: Plz help, Macro needed for If the file opened from folder, do this

  1. #1

    Plz help, Macro needed for If the file opened from folder, do this

    Dear All,

    I need your help. I need to write a macro in the [Sub Workbook_Open()] with if statement stating that if this file opened from folder (C:/Test), then do this.
    If the file opened from other folder, don't do any thing

    Thank you very much

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    can you explain better your goal ?

  3. #3
    Say there is file (called love.xlsm) saved in folder c:/teat. The same file open (love.xlsm) copied to another folder. So if the file love.xlsm opened from folder c:/test, run macro (say clear cell A1 text). but if opened from another folder don't clear A1 text

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If lCase(ThisWorkbook.Path) = "c:\test" then ...

  5. #5
    Quote Originally Posted by Kenneth Hobs View Post
    If lCase(ThisWorkbook.Path) = "c:\test" then ...
    Sorry it is not working for any of bellow. I did the following:
    Private Sub Workbook_Open()
        If LCase(ThisWorkbook.Path) = "C:\Test" Then
            Sheets("Sheet1").Select
            Selection.ClearContents
        Else
            Exit Sub
        End If
    End Sub
    or

    Private Sub Workbook_Open()
         If ThisWorkbook.Path = "C:\Test" Then
             Sheets("Sheet1").Select
             Selection.ClearContents
         Else
             Exit Sub
         End If
     End Sub
    Last edited by loveguy1977; 09-04-2013 at 12:50 PM.

  6. #6
    Sorry guys
    I found my mistake
    it should be like this

    Private Sub Workbook_Open()
    If ThisWorkbook.Path = "C:\Test" Then
        Sheets("Sheet1").Select
        Range("A1").Select
        Selection.ClearContents
    End If
    End Sub

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Private Sub Workbook_Open()      
      If ThisWorkbook.Path = "C:\Test" Then Sheets("Sheet1").Range("A1").ClearContents  
    End Sub

Posting Permissions

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