title: Macro Produces Different Results If Clicking Screen While Running
body: <p>I have two files (Microsoft Excel 97-2003 Worksheets) that contain multiple fields of data. A macro I have will open both files, copy the data (without headers) from one of them, and paste that data underneath the data in the other file. Once the data has been combined, I save the combined file as a new workbook. A somewhat stripped down version of the code I have is below (sorry it's inefficient; I had only just begun VBA when I wrote it):</p>
  
  <pre><code>Sub Combine()
  
  ' Combine Macro
  
  ' Turn some things off to speed things up
  Application.ScreenUpdating = False
  Application.StatusBar = False
  Application.EnableEvents = False
  
  ' Set to directory where files to combine exist
  ChDir "C:\Users\Me\Desktop\Macro Work"
  
  ' Variable Declarations
  Dim EmptyRow As Long
  Dim FileA, FileB As String
  Dim Day as Integer
  
  Day = 1
  
  ' Loop and combine the two files for each day of the month (if they exist)
  Do Until (Day &gt; 31)
  
      ' Get FileA and FileB names
      FileA = "FileA" &amp; Day &amp; ".xls"
      FileB = "FileB" &amp; Day &amp; ".xls"
  
      ' Open files A and B; handle error if file doesn't exist
      On Error Resume Next
      Workbooks.Open FileName:=FileA
          If Err.Number = 1004 Then
              GoTo NoFile
          End If
      Workbooks.Open FileName:=FileB
          If Err.Number = 1004 Then
              GoTo NoFile
          End If
      On Error GoTo 0
  
      'Copy data from FileB and paste into FileA
      EmptyRow = Workbooks(FileA).Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
      Range(Range("A2"), Range("A2").End(xlDown)).Select
      Range(Selection, Selection.End(xlToRight)).Select
      Selection.Copy Workbooks(FileA).Worksheets(1).Range("A" &amp; EmptyRow)
  
      ' Save combined workbook as FileName in Excel format
      Workbooks(FileA).SaveAs FileName:="CombinedFile" &amp; Day, FileFormat:=51
  
      ' Close the workbooks
      Workbooks("CombinedFile" &amp; Day).Close SaveChanges:=False
      Workbooks(FileB).Close SaveChanges:=False
  
  NoFile:
      Day = Day + 1
  
  Loop ' End of Loop
  
  MsgBox "PAUSE - End of Macro"
  
  End Sub
  </code></pre>
  
  <p>My problem isn't that that Macro won't run or throws an error. Rather if I run it and don't do anything else, I get one set of results. But if I run it and click on the VBA Editor Window while it's running, I get a different set of results.</p>
  
  <p>Is it a known fact that you shouldn't do other things on your machine while your macro is running? Or have I neglected to add a piece of code that will keep the results of the macro consistent whether or not I sit and watch or am clicking the VBA Editor Window?</p>
  
  <p>Thanks in advance, and let me know if I can provide further clarification.</p>
  
  <p>Zachary</p>
  
  <p>EDIT:</p>
  
  <p>It's difficult to track down exactly what goes wrong with combining the files, but I can see that the number of lines differ. The combined file generated when I am clicking on the Editor Window has several thousand lines less than when I just watch it run. And I'll need to retract my earlier claim that I can consistently produce the same error. I can consistently reproduce an error, but the results may differ (i.e. maybe I'm missing only 4995 lines instead of 5000).</p>
  
  <p>Liam, your response makes sense to me. However, I have a near identical Macro that skips this section:</p>
  
  <pre><code>'Copy data from FileB and paste into FileA
  EmptyRow = Workbooks(FileA).Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
  Range(Range("A2"), Range("A2").End(xlDown)).Select
  Range(Selection, Selection.End(xlToRight)).Select
  Selection.Copy Workbooks(FileA).Worksheets(1).Range("A" &amp; EmptyRow)
  </code></pre>
  
  <p>Since sometimes the files I work with only have "FileA" I don't combine anything and instead use a Macro to open the file and SaveAs FileFormat=51 and with a new name. Obviously that removes sections in the code where I select data in the file. Yet I still receive different results when clicking. What might be causing this?</p>
  
  <p>Lastly, in addition to removing sections of code where <code>Selection</code> is used, is there a way to help prevent the user from messing up the results of the macro in this way?</p>
  
  <p>Thanks again.</p>