Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using a variabel to make an array?

I´ve worked up this code so far:

Sub PrintFormat()


Dim ws As Worksheet
Dim prt1 As String
Dim prt2 As String
Dim StrArray() As String

prt1 = ""

For Each ws In Worksheets

If InStr(1, ws.Name, "F") 1 Then
prt1 = prt1 & ", " & """" & ws.Name & """"
End If

Next ws

prt1 = Mid(prt1, 3, Len(prt1))
prt2 = Mid(prt1, 2, Len(prt1) - 2)

'Worksheets("ark3").Range("A1") = prt
'"O-002F","O-003F"
Sheets(Array("O-002F", "O-003F")).Select 'this one works
Sheets(Array((prt1))).Select 'this one doesnt work
Sheets(Array(("&prt2"))).Select 'this one doesnt work
End Sub

While debugging it says "Subscript out of range" on either of th
arrays that are done over the variables prt1 and prt2, but holding th
cursor over the variables they seem to be strings with the same conten
as is in the first array. The problem is that over time a lot mor
sheets will be added, and all sheets containging the letter f must b
in the array.

I am sorry if my english is bad ;

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Using a variabel to make an array?

I don't understand what you are doing with Prt2.

Specific to your question...

The documentation on the select method -- if you checked it -- leaves
something to be desired.

Try:

Sheets(prt1).Select
Sheets(prt2).Select(False)

Is the intent to select all the worksheets? If so, use something like:

Sub testIt3()
Dim i As Integer
Sheets(1).Select
For i = 2 To Sheets.Count
Sheets(i).Select False
Next i
End Sub

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article , SkatKat
says...
I=3Fve worked up this code so far:

Sub PrintFormat()


Dim ws As Worksheet
Dim prt1 As String
Dim prt2 As String
Dim StrArray() As String

prt1 = ""

For Each ws In Worksheets

If InStr(1, ws.Name, "F") 1 Then
prt1 = prt1 & ", " & """" & ws.Name & """"
End If

Next ws

prt1 = Mid(prt1, 3, Len(prt1))
prt2 = Mid(prt1, 2, Len(prt1) - 2)

'Worksheets("ark3").Range("A1") = prt
'"O-002F","O-003F"
Sheets(Array("O-002F", "O-003F")).Select 'this one works
Sheets(Array((prt1))).Select 'this one doesnt work
Sheets(Array(("&prt2"))).Select 'this one doesnt work
End Sub

While debugging it says "Subscript out of range" on either of the
arrays that are done over the variables prt1 and prt2, but holding the
cursor over the variables they seem to be strings with the same content
as is in the first array. The problem is that over time a lot more
sheets will be added, and all sheets containging the letter f must be
in the array.

I am sorry if my english is bad ;)


---
Message posted from http://www.ExcelForum.com/


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Using a variabel to make an array?

As you see, your approach doesn't work. You can't build a string that
resembles a valid argument to the array function and convert it to an array
using the Array function.

Sub PrintFormat()


Dim ws As Worksheet
Dim StrArray() As String
Dim i As Long


i = 0
For Each ws In Worksheets

If InStr(1, ws.Name, "F") 1 Then
ReDim Preserve StrArray(0 To i)
StrArray(i) = ws.Name
Debug.Print i, ws.Name, StrArray(i)
i = i + 1
End If

Next ws
Sheets(StrArray).Select

' or
' Sheets(StrArray).Printout
' without selecting

End Sub




"SkatKat " wrote in message
...
I´ve worked up this code so far:

Sub PrintFormat()


Dim ws As Worksheet
Dim prt1 As String
Dim prt2 As String
Dim StrArray() As String

prt1 = ""

For Each ws In Worksheets

If InStr(1, ws.Name, "F") 1 Then
prt1 = prt1 & ", " & """" & ws.Name & """"
End If

Next ws

prt1 = Mid(prt1, 3, Len(prt1))
prt2 = Mid(prt1, 2, Len(prt1) - 2)

'Worksheets("ark3").Range("A1") = prt
'"O-002F","O-003F"
Sheets(Array("O-002F", "O-003F")).Select 'this one works
Sheets(Array((prt1))).Select 'this one doesnt work
Sheets(Array(("&prt2"))).Select 'this one doesnt work
End Sub

While debugging it says "Subscript out of range" on either of the
arrays that are done over the variables prt1 and prt2, but holding the
cursor over the variables they seem to be strings with the same content
as is in the first array. The problem is that over time a lot more
sheets will be added, and all sheets containging the letter f must be
in the array.

I am sorry if my english is bad ;)


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Using a variabel to make an array?

Hi
not really sure what you're trying to do but you're not
really assigning variables to an array but simply
combining strings to a longer string. Maybe the following
is what you're looking for:
Sub ws_in_array()
Dim ws_array()
Dim wks_counter As Integer
Dim i
Dim s

wks_counter = ActiveWorkbook.Worksheets.Count
ReDim ws_array(1 To wks_counter)
s = 0
For i = 1 To wks_counter
If InStr(Worksheets(i).Name, "t") 0 Then
s = s + 1
ws_array(s) = Worksheets(i).Name
End If
Next
ReDim Preserve ws_array(1 To s)
For i = 1 To UBound(ws_array)
MsgBox ws_array(i)
Next
End Sub



-----Original Message-----
I´ve worked up this code so far:

Sub PrintFormat()


Dim ws As Worksheet
Dim prt1 As String
Dim prt2 As String
Dim StrArray() As String

prt1 = ""

For Each ws In Worksheets

If InStr(1, ws.Name, "F") 1 Then
prt1 = prt1 & ", " & """" & ws.Name & """"
End If

Next ws

prt1 = Mid(prt1, 3, Len(prt1))
prt2 = Mid(prt1, 2, Len(prt1) - 2)

'Worksheets("ark3").Range("A1") = prt
'"O-002F","O-003F"
Sheets(Array("O-002F", "O-003F")).Select 'this one works
Sheets(Array((prt1))).Select 'this one doesnt work
Sheets(Array(("&prt2"))).Select 'this one doesnt work
End Sub

While debugging it says "Subscript out of range" on

either of the
arrays that are done over the variables prt1 and prt2,

but holding the
cursor over the variables they seem to be strings with

the same content
as is in the first array. The problem is that over time a

lot more
sheets will be added, and all sheets containging the

letter f must be
in the array.

I am sorry if my english is bad ;)


---
Message posted from http://www.ExcelForum.com/

.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Using a variabel to make an array?


"SkatKat " wrote in message
...
prt1 = Mid(prt1, 3, Len(prt1))
prt2 = Mid(prt1, 2, Len(prt1) - 2)


What is this supposed to do.


'Worksheets("ark3").Range("A1") = prt
'"O-002F","O-003F"
Sheets(Array("O-002F", "O-003F")).Select 'this one works
Sheets(Array((prt1))).Select 'this one doesnt work


Assuming you cut out those two lines above, this will work

Sheets(Array(SPlit(prt1,"&"))).Select

Sheets(Array(("&prt2"))).Select 'this one doesnt work


No chance, none of the worksheet start with &.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
countif with variabel criteria? Hans Excel Worksheet Functions 2 January 23rd 08 06:57 AM
Make formula more simple --- array? Nikki Excel Discussion (Misc queries) 6 April 11th 07 07:21 PM
Can I make array position A(12) into a variable A(12*n) ? Paul Excel Worksheet Functions 1 October 9th 06 10:37 PM
getting the sum of variabel ranges Hein Excel Worksheet Functions 4 March 26th 06 07:44 PM
The # in my vector is absent from my array. Can I make it 0? Matherley Excel Worksheet Functions 1 July 14th 05 08:52 PM


All times are GMT +1. The time now is 05:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"