+ Reply to Thread
Results 1 to 7 of 7

VBA Code Error: Compile Error Invalid Outside Procedure

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2023
    Location
    Turkiye
    MS-Off Ver
    2003
    Posts
    1

    VBA Code Error: Compile Error Invalid Outside Procedure

    Hi .

    i created vba code and get this error : Compile Error Invalid Outside Procedure

    I know to start SUB and end as END SUB but i do not know where i did wrong.
    I also used private SUB instead of only SUB but not working



    Code is sth like this

     Sub MVR_SEDAT()
    
        Dim file As Variant
        Dim path As String
        Dim folderPath As String
        Dim fileName As String
        Dim searchName As String
        
        ' Set the path to the desktop folder
        path = Environ("USERPROFILE") & "\Desktop"
        
        ' Set the folder path to move the files to
        folderPath = "C:\Users\sedatcan\Desktop\SEDAT"
        
        ' Set the string to search for in the file names
        searchName = "SEDAT"
      
        
        ' Loop through all files in the desktop folder
        For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(path).Files
            
            ' Check if the file name contains the search string
            If InStr(file.Name, searchName) > 0 Then
            
                ' Get the file name
                fileName = file.Name
                
                ' Move the file to the destination folder
                FileCopy path & fileName, folderPath & fileName
                
                ' Delete the original file from the desktop
                Kill path & fileName
                
            End If
            
        Next file
        
        ' Show a message when the operation is complete
        MsgBox "All files containing " & searchName & " have been moved to " & folderPath
        
    End Sub
    Last edited by alansidman; 05-18-2023 at 03:53 PM.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: VBA Code Error: Compile Error Invalid Outside Procedure

    Hi, welcome to the forum.
    Please read the forum rules of how to mark code in your post.

    I have not tested you code but I imagine you're missing the final backslash in the path declarations

    folderPath= "C:\Users\sedatcan\Desktop\SEDAT\"
    Also check all the other path references
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: VBA Code Error: Compile Error Invalid Outside Procedure

    There is nothing wrong (at compile time) with the code you have put up in this thread.
    There must be something else either in another sheet or another code module or the same sheet/module, maybe left over from something you partially deleted?

    How about putting the workbook up if you can't find it?

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: VBA Code Error: Compile Error Invalid Outside Procedure

    As Croweater has said, there is nothing wrong with the code at compile time.

    Option Explicit
    
    Sub MVR_SEDAT()
    
    Dim file As Variant
    Dim path As String
    Dim folderPath As String
    Dim fileName As String
    Dim searchName As String
    
    ' Set the path to the desktop folder
    path = Environ("USERPROFILE") & "\Desktop"
    
    ' Set the folder path to move the files to
    folderPath = "C:\Users\sedatcan\Desktop\SEDAT"
    
    ' Set the string to search for in the file names
    searchName = "SEDAT"
    
    ' Loop through all files in the desktop folder
    For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(path).Files
    
        ' Check if the file name contains the search string
        If InStr(file.Name, searchName) > 0 Then
        
            ' Get the file name
            fileName = file.Name
            
            ' Move the file to the destination folder
            FileCopy path & fileName, folderPath & fileName
            
            ' Delete the original file from the desktop
            Kill path & fileName
    
        End If
    
    Next file
    
    ' Show a message when the operation is complete
    MsgBox "All files containing " & searchName & " have been moved to " & folderPath
    
    End Sub
    If this is the only code/module, then there could be "stray" characters before or after the subroutine that are confusing the compiler. For example, a single comma after the End Sub would give you a Compile error: Syntax error.

    Maybe cut and paste just the code to a new module and then remove the original module.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: VBA Code Error: Compile Error Invalid Outside Procedure

    See these recent solved threads:
    https://www.excelforum.com/excel-pro...procedure.html

    And: https://www.excelforum.com/excel-pro...procedure.html

    Also look at the list of Similar Threads shown below.

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: VBA Code Error: Compile Error Invalid Outside Procedure

    Code compiles fine then there's some more code elsewhere in the module that is not posted.

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,671

    Re: VBA Code Error: Compile Error Invalid Outside Procedure

    @usedat

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)

    However, if you continue to not use code tags, you can expect to have your thread BLOCKED until you add them yourself.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Compile Error: Invalid outside procedure
    By sas5610 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2023, 03:13 AM
  2. compile error: invalid outside procedure
    By vbanoob1999 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-01-2020, 10:37 AM
  3. Compile Error: Invalid outside procedure
    By ANewCoder in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-05-2019, 04:31 PM
  4. Compile Error: Invalid Outside Procedure Error
    By pschris in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2015, 01:51 PM
  5. “Compile error: Invalid Outside Procedure”
    By Rajeshkumar R in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-26-2013, 04:32 AM
  6. [SOLVED] Compile Error: Invalid Outside Procedure
    By packet25 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-13-2013, 07:02 AM
  7. Compile Error: Invalid outside procedure
    By smylod in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2009, 01:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1