Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Build an string for an array

Posted on 2012-03-27
7
Medium Priority
?
373 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 35

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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 35

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

636 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