[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Build an string for an array

Posted on 2012-03-27
7
Medium Priority
?
374 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

834 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