?
Solved

Build an string for an array

Posted on 2012-03-27
7
Medium Priority
?
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 34

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 34

Accepted Solution

by:
Norie earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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