【问题标题】:VBA Excel - .Window doesnt recognise open excel windowsVBA Excel - .Window 无法识别打开的 Excel 窗口
【发布时间】:2016-06-21 15:28:15
【问题描述】:

如果文件名与命名约定接近,下面的代码将循环打开打开的 excel 文件,然后运行 ​​if 语句中的代码,以便以后保存文件

此代码在 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 个窗口(带有实际活动工作簿的标题).. . 然后你应该为工作簿中的每个项目运行(以获取所有打开的工作簿名称)

标签: vba excel excel-2010


【解决方案1】:

对于多个 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

【讨论】:

  • 打我一拳!这就是我会做的。
  • @ghg565 以为你有它,但它做的事情和以前一样,它只得到一个文件名并移动到循环的下一个迭代。 For 循环甚至循环它只是在它面前运行
  • @ghg565 它是,它获取我正在运行代码的 Excel 文档的名称,然后转到 end if > next,然后进入下面的行
  • 在您的 VBA 编辑器中,项目资源管理器窗口 Ctrl-R 是否显示多个项目?如果没有,那么 ghg565 的代码工作正常。
  • 如果它只显示一个项目,那么您需要参考stackoverflow.com/questions/2971473/…以获得更多帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2020-03-02
  • 2022-01-24
  • 2016-10-22
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
相关资源
最近更新 更多