Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 90
Default Trouble with variable type

G'day there Once Again,

I've received some great advice here on this list, most recently
from the list archives. It's amazing how much information is in there
that I didn't have to ask about specifically.

This latest problem of mine, though, doesn't seem to be addressed
specifically. Or if it is, then I've not used the correct terminology in
my searches and missed it. Hopefully, someone can point me in the right
direction.

I'm using the GetOpenFilename method to load a list of filenames
into a variable of type Variant:

Option Explicit

Sub Text_In()

Dim fileList As Variant
Dim x As Integer
Dim newSht As Worksheet
Dim UF As String, fName As String

'
' Text_In Macro
' Macro recorded by Ken McLennan
'

x = 1

fileList = Application.GetOpenFilename(, , , , True)

If CBool(fileList) < False Then <----- ****
' Parse list and open worksheets for each file
Do
fName = Mid(fileList(x), InStrRev(fileList(x), "\") + 1)

Set newSht = Worksheets.Add
newSht.Name = fName

x = x + 1
Loop Until x = UBound(fileList) + 1
End If

' Code found in archives - I think by Tom Ogilvy

' Dim UF As String
' Dim FName As String
' FName = Mid(UF, InStrRev(UF, "\") + 1)
' Workbooks(FName).Activate

End Sub


I find that all works well up to the line with <----- ****.

The CBool() is only one of my attempts to get it to work. By
modifying that line I can have the routine either enter the selected
filenames, or pass the 'Cancel' option through to completion.

'fileList' becomes an array of strings if a file is selected (a
single element array if there's only one file), or a boolean containing
'false' if the file dialog is cancelled.

If I set the <----- **** line to work OK with one option, I get a
"Type Mismatch Error" for the other. I.e. if I get it accepting the
Boolean value, it won't work with the string, and vice versa.

How can I get it to accept either Boolean (for cancel) *and*
String (for selected files) data types?

I'm sure that it can be done, but it's giving me a headache.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Trouble with variable type

Ken,

Use the IsArray function to test the fileList variable. If it
returns False, then fileList is not an array, and you can safely
assume that the user pressed Cancel. Otherwise, if IsArray
returns True, fileList contains an array of file names. E.g.,

Dim fileList As Variant
Dim Ndx As Long
fileList = Application.GetOpenFilename(MultiSelect:=True)
If IsArray(fileList) = False Then
' user cancelled
Debug.Print "Cancel"
Else
For Ndx = LBound(fileList) To UBound(fileList)
Debug.Print fileList(Ndx)
Next Ndx
End If



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Ken McLennan" wrote in message
.. .
G'day there Once Again,

I've received some great advice here on this list, most

recently
from the list archives. It's amazing how much information is in

there
that I didn't have to ask about specifically.

This latest problem of mine, though, doesn't seem to be

addressed
specifically. Or if it is, then I've not used the correct

terminology in
my searches and missed it. Hopefully, someone can point me in

the right
direction.

I'm using the GetOpenFilename method to load a list of

filenames
into a variable of type Variant:

Option Explicit

Sub Text_In()

Dim fileList As Variant
Dim x As Integer
Dim newSht As Worksheet
Dim UF As String, fName As String

'
' Text_In Macro
' Macro recorded by Ken McLennan
'

x = 1

fileList = Application.GetOpenFilename(, , , , True)

If CBool(fileList) < False Then <----- ****
' Parse list and open worksheets for each file
Do
fName = Mid(fileList(x), InStrRev(fileList(x),

"\") + 1)

Set newSht = Worksheets.Add
newSht.Name = fName

x = x + 1
Loop Until x = UBound(fileList) + 1
End If

' Code found in archives - I think by Tom Ogilvy

' Dim UF As String
' Dim FName As String
' FName = Mid(UF, InStrRev(UF, "\") + 1)
' Workbooks(FName).Activate

End Sub


I find that all works well up to the line with <----- ****.

The CBool() is only one of my attempts to get it to work. By
modifying that line I can have the routine either enter the

selected
filenames, or pass the 'Cancel' option through to completion.

'fileList' becomes an array of strings if a file is selected (a
single element array if there's only one file), or a boolean

containing
'false' if the file dialog is cancelled.

If I set the <----- **** line to work OK with one option, I get

a
"Type Mismatch Error" for the other. I.e. if I get it accepting

the
Boolean value, it won't work with the string, and vice versa.

How can I get it to accept either Boolean (for cancel) *and*
String (for selected files) data types?

I'm sure that it can be done, but it's giving me a headache.



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
Having trouble getting MATCH to work with a variable lookup array Chuck M Excel Worksheet Functions 3 August 20th 08 11:45 PM
Trouble setting variable as filename with date Elby Excel Programming 2 February 21st 04 03:05 PM
efine the data type of a variable monika Excel Programming 0 February 13th 04 02:01 AM
type variable as argument of a sub Koos Excel Programming 1 October 23rd 03 11:41 AM
Variable Type - help me to solve this choco140 Excel Programming 1 September 28th 03 10:39 AM


All times are GMT +1. The time now is 07:31 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"