为每个打开的工作簿执行功能

时间:2019-05-08 20:01:12

标签: excel vba excel-2016

我正在创建一个带有命令按钮的空白工作簿,单击该按钮后,我希望它对当前打开的每个打开的工作簿执行操作(因为我还有其他并非空白的工作簿,因此我希望它对它们进行操作) )。

运行时出现下标超出范围错误:

Sub Button1_Click()
'
' Button1_Click Macro
' Fire Ext. Comments
'
'
Dim w As Workbook

' For every open workbook...
For Each w In Application.Workbooks

    ' Activate the current workbook
    w.Activate

    ' Find the comments column (K12 should be the "Comments" column)
    If Worksheets("FIRE EXT.").Range("K12").Value = "Comments" Then

        ' Then we loop through all cells in the specified range (anything below the header row)
        Dim rng As Range, cell As Range

        ' I'm using a range of 500 to look for values, so if a file has more than 500 rows, you'll have to look at it manually
        Set rng = Range("A1:A500")

        ' No loop to change all comments
        For Each cell In rng
.......................

...位于“ If工作表(“ FIRE EXT。”)。范围(“ K12”)。Value =“ Comments” Then “行中。因此,我认为这是从空白工作簿开始的,而不是找到名为“ FIRE EXT。”的工作表,因此最好的方法是首先测试激活的工作簿是否首先具有该工作表的名称,否则转到下一个工作簿。 ?谢谢!

更新

这就是对我有用的方法,但是其他答复也可以起作用。谢谢大家!

Sub Button1_Click()
'
' Button1_Click Macro
' Fire Ext. Comments
'
'
Dim w As Workbook

' For every open workbook...
For Each w In Application.Workbooks

    ' Don't work on the current/blank workbook
    If w.FullName <> ThisWorkbook.FullName Then

    ' Find the comments column (K12 should be the "Comments" column)
    If w.Sheets("FIRE EXT.").Range("K12").Value = "Comments" Then

        ' Then we loop through all cells in the specified range (anything below the header row)
        Dim rng As Range, cell As Range

        ' I'm using a range of 500 to look for values, so if a file has more than 500 rows, you'll have to look at it manually
        Set rng = w.Worksheets("FIRE EXT.").Range("A13:A500")

        ' No loop to change all comments
        For Each cell In rng

2 个答案:

答案 0 :(得分:2)

您需要完全限定所有参考文献。您还可以检查一下以确保它跳过空白工作簿(请参阅此更新代码中的注释):

Sub Button1_Click()
'
' Button1_Click Macro
' Fire Ext. Comments
'
'
Dim w As Workbook

' For every open workbook...
For Each w In Application.Workbooks

    If w.FullName <> ThisWorkbook.FullName Then     '<-- TA: Add check to verify you're not working on the blank workbook

        'TA: I removed the .Activate line, that is never necessary.  Instead, fully qualify all your references

        ' Find the comments column (K12 should be the "Comments" column)
        If w.Worksheets("FIRE EXT.").Range("K12").Value = "Comments" Then   '<-- TA: Note that Worksheets is now qualified to w so that it is checking worksheets in the current w workbook

            ' Then we loop through all cells in the specified range (anything below the header row)
            Dim rng As Range, cell As Range

            ' I'm using a range of 500 to look for values, so if a file has more than 500 rows, you'll have to look at it manually
            Set rng = w.Worksheets("FIRE EXT.").Range("A1:A500")    '<-- TA: Note that Range is now qualified to w.Worksheets("FIRE EXT.") (if that isn't the correct sheet name, change this to the correct sheet name)

            ' Now loop to change all comments
            For Each cell In rng
.......................

答案 1 :(得分:1)

我想输入评论,但是它太长了。

  1. 如果工作表名称为“ FIRE EXT”,则会出现错误。 (带有句号)在您正在使用的工作簿上不存在。您正在循环所有工作簿,如果您有一个没有该工作簿的工作簿,它将出错。
  2. 使用工作表名称时最好坚持使用Sheets,使用工作表编号时最好坚持使用WorksheetsSheets("SheetName")Worksheets(1)
  3. 避免在代码中使用分配的工作簿变量来使用“激活/选择”,即“ w”

    Sub Button1_Click()
    Dim w As Workbook
    
    ' For every open workbook...
    For Each w In Application.Workbooks
    
        ' Find the comments column (K12 should be the "Comments" column)
        If w.Sheets("FIRE EXT.").Range("K12").Value = "Comments" Then
    
            ' Then we loop through all cells in the specified range (anything below the header row)
            Dim rng As Range, cell As Range
    
            ' I'm using a range of 500 to look for values, so if a file has more than 500 rows, you'll have to look at it manually
            Set rng = w.Sheets("FIRE EXT.").Range("A1:A500")
    
            ' Now loop to change all comments
            For Each cell In rng
            ' Now here you dont use "w.Sheets("FIRE EXT.")" because it is already set on `rng'
            ' so you can just use `cell` like cell.value = "Blah"
    .........