VBA Create and Name Worksheets Dynamically

Thank you for looking at my question,

I am extracting data from a text file into an excel file (.xlsx)

I want to break the data down into 1 worksheet per item

within my code I tried using this approach:

        '' Generic Item
        If Left(strLineInput, 13) = "Generic Item:" Then
            arrLine_Process_One = Split(strLineInput, ":", , vbTextCompare)
            strItem = Trim(arrLine_Process_One(1))
            If strItem <> strItem_Old Then
                strItem_Old = strItem
                ' Set the Active Worksheet
                Set objSheetObject = objExcelObject.ActiveWorkbook.Worksheets(strItem)
                '' Set Header Row
                With objSheetObject
                    .Cells(1, 1) = "Item"
                    .Cells(1, 2) = "Item Desc"
                    .Cells(1, 3) = "Operation"
                    .Cells(1, 4) = "Step"
                    .Cells(1, 5) = "Eff Date"
                    .Cells(1, 6) = "Task"
                    .Cells(1, 7) = "Task Desc"
                    .Cells(1, 8) = "Work Centre"
                    .Cells(1, 9) = "WC Desc"
                    .Cells(1, 10) = "Machine"
                    .Cells(1, 11) = "Machine Desc"
                    .Cells(1, 12) = "Setup"
                    .Cells(1, 13) = "Run Time"
                    .Cells(1, 14) = "Matrix No"
                    .Cells(1, 15) = "Matrix Desc"
                    .Cells(1, 16) = "Matrix"
                    .Cells(1, 17) = "Comment"
                    .Cells(1, 18) = "Info"
                    With .Range("A1:R1").Interior
                         .Pattern = xlSolid
                         .Color = RGB(142, 169, 219)
                    End With
                End With
            End If

Open in new window

but the code falls over at  Set objSheetObject = objExcelObject.ActiveWorkbook.Worksheets(strItem)
telling me the 'subscript is out of range'

Please can somebody tell me how to achieve my aim
Gary CroxfordOperations Support AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


 what is the value of strItem

strItem = Trim(arrLine_Process_One(1))

If you want the first item of the array use 0

Gary CroxfordOperations Support AnalystAuthor Commented:

an example of the value of strLineInput is    'Generic Item:VE01'

there are almost 3000 lines in the source data relating to 41 generic items - VE01 to VE41
So the Worksheet you are trying to assign is named VE01, isn't it?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Gary CroxfordOperations Support AnalystAuthor Commented:
yes, the first would be ve01, the next ve02 etc
So you want to create new Worksheets

then try

Set objSheetObject = objExcelObject.ActiveWorkbook.Worksheets.Add _
objSheetObject.Name = strItem

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gary CroxfordOperations Support AnalystAuthor Commented:
Excellent, Thank you very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.