Solved

Build an string for an array

Posted on 2012-03-27
7
358 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 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

726 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