Copy the element throughout the whole workbook with exclusion some sheets

眉间皱痕 提交于 2021-02-11 14:15:06

问题


I would like to copy the element throughout the whole workbook, keeping one sheet excluded (the very 1st one).

Following the query here: Copy the element throughout a whole workbook

and the advice here:

VBA - Loop Through All Sheets And Exclude One

I prepared the code as per below:

 Sub asbuiltcopy()

 Dim Ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet, s As Shape

 Set ws1 = Worksheets("Frontsheet")      'sheet containing original textbox
 Set ws2 = Worksheets("Readme")
 Set s = ws1.Shapes("AsBuiltBox")     'name of original textbox

 Application.ScreenUpdating = False

 For Each Ws In Worksheets
   If Ws.Name <> ws1.Name Then
    s.Copy
    Ws.Paste
    Ws.Shapes(Ws.Shapes.Count).Top = s.Top
    Ws.Shapes(Ws.Shapes.Count).Left = s.Left
   End If
   If Ws.Name <> ws2.Name Then
    s.Delete
  End If
 Next Ws

Application.ScreenUpdating = True


End Sub

But the debugger says, that an object is required.

I changed my code slightly:

 Sub asbuiltcopy()

 Dim Ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet, s As Shape

 Set ws1 = Worksheets("Frontsheet")      'sheet containing original textbox
 Set ws2 = Worksheets("Readme")
 Set s = ws1.Shapes("AsBuiltBox")     'name of original textbox

 Application.ScreenUpdating = False

 For Each Ws In Worksheets
 If Ws.Name <> ws1.Name Then
    s.Copy
    Ws.Paste
    Ws.Shapes(Ws.Shapes.Count).Top = s.Top
    Ws.Shapes(Ws.Shapes.Count).Left = s.Left
 End If

 Next Ws

 Application.ScreenUpdating = True

 With ws2
 s.Delete
 End With

End Sub

But in this case, I am losing the stamp on my 2nd sheet instead of the first one, which I want to exclude.

The last attempt to the code was:

 Sub Asbuiltcopy()

 Dim Ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet, s As Shape

 Set ws1 = Worksheets("Frontsheet")      'sheet containing original textbox
 Set ws2 = Worksheets("Readme")
 Set s = ws1.Shapes("AsBuiltBox")     'name of original textbox

 ws2.Hidden = True

 Application.ScreenUpdating = False

 For Each Ws In Worksheets
  If Ws.Name <> ws1.Name Then
    s.Copy
    Ws.Paste
    Ws.Shapes(Ws.Shapes.Count).Top = s.Top
    Ws.Shapes(Ws.Shapes.Count).Left = s.Left
 End If
 Next Ws

 Application.ScreenUpdating = True

 Call AsReadmeremove

 ws2.Hidden = False

 End Sub

But now I am getting:

Compile error: Method or data member not found - pointing the ws2.Hidden = True line.

What can I do to make the element gone only on the very first sheet instead of on 2nd one?

来源:https://stackoverflow.com/questions/60510387/copy-the-element-throughout-the-whole-workbook-with-exclusion-some-sheets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!