循环工作簿中的所有工作表不起作用 [英] Loop for all worksheet in a workbook is not working

查看:25
本文介绍了循环工作簿中的所有工作表不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过工作簿中所有工作表的最后一行复制粘贴工作表的第一个单元格,但我的代码不起作用,代码仅在活动工作表上完成.

I would to copy paste the first cell of a sheet trough the last row for all sheets in a workbook and my code is not working, the code is done on the active sheet only.

Sub Macro5()
'
' Macro5

 Dim ws As Worksheet

 For Each ws In ActiveWorkbook.Worksheets

     Range("A2").Copy Destination:=Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)

 Next ws

End Sub

感谢您的帮助

推荐答案

循环工作表不会使它们处于活动状态.你想要这样的东西.

Looping the worksheets does not make them active. You want something like this.

    Dim WS As Excel.Worksheet
    Dim iIndex As Integer

    For iIndex = 1 To ActiveWorkbook.Worksheets.count
        Set WS = Worksheets(iIndex)
        With WS

        'Do something here.
            .Range("A2").Copy Destination:=.Range("A3:A" & .Cells(.Rows.Count, "B").End(xlUp).Row)
        End With
    Next iIndex

你可以在循环中使用 ws,但我总是设置对象.

You could use the ws in your loop, but I always set the object.

 For Each ws In ActiveWorkbook.Worksheets
     ws.Range("A2").Copy Destination:=ws.Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
 Next ws

这篇关于循环工作簿中的所有工作表不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆