2016-06-21 53 views
1

下面的代码通过打开的excel文件循环,如果文件名是命名约定接近的话,它将运行if语句中的代码,以便文件可以稍后保存VBA Excel - .Window无法识别打开的Excel窗口

此代码适用于Excel 2003,但不适用于Excel 2010,查看代码的每个部分myWindow.Caption只显示1个文件名,而不是5个文件名。我错过了什么来循环2010年的所有文件?

仅供参考 - 有For循环的多个实例,但因为它都是相同的代码,我没有粘贴在这里。让我知道如果你想要它,但它是足够接近相同

Sub File_Saver() 
    Dim iFileCount As Integer 
    Dim myWindow As Window 
    Dim r As Integer 

    With Application 
     .DisplayAlerts = False 
     .ScreenUpdating = False 
    End With 

    For Each myWindow In Application.Windows 
     If LCase(myWindow.Caption) Like LCase("CHL?ISS*") Then 

      iFileCount = 1 
      r = 21 

      myWindow.Activate 
      'Set Column 
      c = B 

      GoTo Continue 
      Exit For 
     End If 
    Next myWindow 
+1

要正常工作,您需要运行应用程序的多个实例...只需打开所有文件,通常会将它合并到1个窗口(带有实际活动工作簿的标题)...您应该为每个项目运行然后在工作簿中(获取所有打开的工作簿名称) –

回答

3

对于多个Excel实例来看看这个答案: Can VBA Reach Across Instances of Excel?

这适用于Excel的一个实例:

Dim workBooks as Workbooks  
Dim book As Workbook 
Set workBooks = Application.Workbooks 
For Each book In workbooks 
    If LCase(book.Name) Like LCase("CHL?ISS*") Then 
     iFileCount = 1 
     r = 21 
     book.Activate 
     c = B 
     ' Not sure where the Continue: statement is 
     GoTo Continue 
Exit For 
    End If 
Next 
+0

打败我!这是我会做的。 – BGeorge

+0

@ ghg565认为你已经拥有了它,但它与之前一样,它只获得一个文件名并移到循环的下一个迭代中。 'For'循环dosnt even循环它只是跑过海峡 –

+0

它执行If语句内的代码吗? – ghg565