Solved

Build an string for an array

Posted on 2012-03-27
7
354 Views
1 Endorsement
Last Modified: 2012-03-27
I have this bit of code that loops through all the sheets of an Excel Spread sheet. it works fine. However when I get to appXL.Sheets(Array(strArray)).Select I get the out of subscript error. I can see that the string I am building is the problem. It is a string that begins and ends with quotation marks and that will not work. Any ideas on how to get around this? The goal is to get the array and then add the new sheets and delete the list of the old ones.

Function SheetNames()
Dim strSheet As String
Dim j As Integer
Dim strSheets As String
Dim strArray As String

j = 1
With appXL
.DisplayAlerts = False
     For Each wks In ActiveWorkbook.Worksheets
        strSheet = Chr(34) & .Sheets(j).NAME & Chr(34) & ","
    j = j + 1
    strSheets = strSheets + strSheet
    Next wks

End With

If strSheet <> "" Then
strArray = Left(strSheets, Len(strSheets) - 1)
End If

appXL.Sheets(Array(strArray)).Select
ActiveWindow.SelectedSheets.Delete
    
appXL.DisplayAlerts = True

End Function

Open in new window


Thank you for your help
Function SheetNames() As String
Dim shtArr()
Dim strSheet As String
Dim j As Integer
Dim shtCount As Integer
Dim strSheets As String
Dim strArray As String

j = 1
With appXL
.DisplayAlerts = False
     For Each wks In ActiveWorkbook.Worksheets
        strSheet = Chr(34) & .Sheets(j).NAME & Chr(34) & ","
        'Debug.Print .Sheets(j).NAME
        '.Sheets(strSheet).Select
        'ActiveWindow.SelectedSheets.Delete
    j = j + 1
    strSheets = strSheets + strSheet
    Next wks

End With

If strSheet <> "" Then
strArray = Left(strSheets, Len(strSheets) - 1)
End If

    appXL.Sheets(Array(strArray)).Select
    ActiveWindow.SelectedSheets.Delete
    
'.DisplayAlerts = True

End Function

Open in new window

1
Comment
Question by:JArndt42
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 37774250
Not sure which code you want looked at but you could try this.

Sub SheetNames()
Dim strSheet As String
Dim j As Integer
Dim strSheets As String
Dim strArray

j = 1
With Application
.DisplayAlerts = False
     For Each wks In ActiveWorkbook.Worksheets
        strSheet = .Sheets(j).Name & ","
    j = j + 1
    strSheets = strSheets + strSheet
    Next wks

End With

If strSheet <> "" Then

     strArray = Split(Left(strSheets, Len(strSheets) - 1), ",")


     xlApp.Sheets(strArray).Delete

End If

appXL.DisplayAlerts = True

End Sub

Open in new window

You can try that but t wil give you an error here.
     xlApp.Sheets(strArray).Delete

Open in new window

The reason for that isn't a problem with the array but that you are trying to delete every worksheet in the workbook, which Excel won't allow - you must have at least one worksheet.
0
 
LVL 1

Author Comment

by:JArndt42
ID: 37774290
the example on the top was the cleaned up version of the second one. Not sure why it put the second one on here as I thought I deleted it before I submitted the question. What I want to do is build the string for the appxl.sheets(strarray).Delete for another place in my code. I am going to create some sheets after I get this array and then delete these. so for instance somewhere else I will have something like.

strDeleteSheets = sheetnames
sheets.add
appxl.sheets(strDeleteSheets).delete

So really I am looking to do it this

Function SheetNames() As String
Dim strSheet As String
Dim j As Integer
Dim strSheets As String

j = 1
With appXL
     For Each wks In ActiveWorkbook.Worksheets
        strSheet = Chr(34) & .Sheets(j).NAME & Chr(34) & ","
    j = j + 1
    strSheets = strSheets + strSheet
    Next wks
End With

If strSheet <> "" Then
SheetNames = Left(strSheets, Len(strSheets) - 1)

End Function

Open in new window

0
 
LVL 1

Author Comment

by:JArndt42
ID: 37774294
Also I got an error on your Split(Left(strSheets, Len(strSheets) - 1), ",") line. it said type mismatch.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 33

Expert Comment

by:Norie
ID: 37774306
You can't use a string like you have with Sheets you need an array.
0
 
LVL 1

Author Comment

by:JArndt42
ID: 37774311
How do I build an array with the sheet names?
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 37774343
That's what the code does using Split.

I think I might know why the code didn't work for you - I changed a few things to tes it.

Anyway, here it is as a function which returns an array.
Function SheetNames() As Variant
Dim strSheet As String
Dim j As Integer
Dim strSheets As String
Dim strArray

j = 1
With xlApp

     For Each wks In ActiveWorkbook.Worksheets
        strSheet = .Sheets(j).Name & ","
         j = j + 1
         strSheets = strSheets + strSheet
    Next wks

End With

If strSheet <> "" Then
      strArray = Split(Left(strSheets, Len(strSheets) - 1), ",")
      SheetNames = strArray   
End If


End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:JArndt42
ID: 37774373
That worked like a charm. so you use split to make it an array then? and Dim it as a variant.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now