Fixing Excel VBA (Macro) Compile Error For Next, Do Loop, While Wend

preview_player
Показать описание
Grab the Free VBA Quick Reference Guide
If a
1) A For doesn't have a next
A next doesn't have a for
2) A Do doesn't have a Loop
a loop doesn't have a do
3) A While doesn't have Wend
or a Wend doesn't have a why.

Sounds confusing watch the video it will make more sense
Рекомендации по теме
Комментарии
Автор

Hello, I have an issue with a loop. I have an End IF within my loop. I would like to add loop in another file with the exact same code. However I am getting the message for a Compile Error without Do. Here is my code with loop. FYI: This is my first time writing macros.  Sub Auto_Open()
 
 
 Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder As Object
    Dim strFilename As String
    Dim dteFile As Date
       
    Const myDir As String = 2016"
   
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
   
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
   
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
        If objFile.DateLastModified > dteFile Then
            dteFile = objFile.DateLastModified
            strFilename = objFile.path
     End If
    Next objFile
 
    Workbooks.Open strFilename
           
    Set FileSys = Nothing
    Set myFolder = Nothing
  
   
  End If
   
   Loop
 Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder As Object
    Dim strFilename As String
    Dim dteFile As Date
       
    Const myDir As String = "G:\S\Staffing\Headcount\July 2016 "
   
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
   
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
   
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
        If objFile.DateLastModified > dteFile Then
            dteFile = objFile.DateLastModified
            strFilename = objFile.path
     End If
    Next objFile
 
    Workbooks.Open strFilename
           
    Set FileSys = Nothing
    Set myFolder = Nothing
  
   
  End If
   
   Loop  End Sub

martricaallumns
Автор

give the solution instead of telling problems ?

MV-sybz