Without switching (using activating/select), add a sheet to Thisworkbook not working

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Code is several procedures/functions, too long to post and haven't quite found required answer through several searches online.

Workbook macro opens a file, extract data from opened file and then creates output sheets in the workbook macro is run from:

Rich (BB code):
Sub CreateOutput(byref dic as variant)

Dim wkb as Workbook
Dim x     as Long

Set wkb = Workbooks.Open(a path)

For x = 0 to dic.Count - 1
   CreateSheet dic.Items()(x), wkbSource
Next x

wkb.Close False
Set wkb = Nothing

End Sub




Private sub CreateSheet(ByRef arr As Variant, ByRef wkbSource As Workbook)

 ThisWorkbook.Worksheets.add after:=(ThisWorkbook.Worksheets.Count)

'Other code

End Sub
Line in red: run-time error 1004 Method 'Add' of objects 'Sheets' Failed

This doesn't work either, same error:
ThisWorkbook.Worksheets.add after:=(Worksheets.Count)

When the private Sub is called, the prior opened file is active, I'm trying to avoid using Thisworkbook.Activate as shouldn't be a need for it.

What's the correct syntax please? I'm guessing the compiler isn't understanding the request as asked.

Thanks in advance,
Jack
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This syntax works IF I use Thisworkbook.Activate after opening source data file
Code:
ThisWorkbook.Activate
ThisWorkbook.Worksheets.Add after:=ThisWorkbook.Sheets(Worksheets.Count)
 
Last edited:
Upvote 0
Code:
ThisWorkbook.Worksheets.Add after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)

should work without activating.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,392
Members
449,445
Latest member
JJFabEngineering

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top