Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default easy way to check for forbidden characters?


Hi all,

Is there an easy way to check for forbidden windows characters in a
script that takes the output of a textbox to use in a save as script?


Code:
--------------------
Private Sub CommandButton1_Click()
Dim studyName As String
studyName = Trim(txbStudyName.Value)
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End Sub

--------------------


where txbStudyName is the name of a textbox in a user form and I want
to check studyName for any of the forbidden windows filename
characters.

Only way I've managed to do it is like so:


Code:
--------------------
Private Sub CommandButton1_Click()
Dim studyName As String
Dim badChar1 As String
studyName = Trim(txbStudyName.Value)

badChar1 = InStr(1, studyName, "<")

If badChar1 < "" Then
MsgBox "Study name is invalid."
Else
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End If
End Sub

--------------------


That just looks for a "<" character, and if found, creates an output.
Then if there is an output, throw a warning. I've had to make a
separate variable and elseif statement for each character, which makes
for rather cumbersome code. So I was wondering if there was a built-in
feature that would do this for me with just a line or two of code.

While I'm at is, is there a way to make VBA check the save file path
for any files with the same name before trying to save? i.e. if there
is already a file called "Study101.xls" in the destination path, have
VBA code throw the error instead of excel?

Thanks for yer help

--Ouka


--
Ouka
------------------------------------------------------------------------
Ouka's Profile: http://www.excelforum.com/member.php...o&userid=23988
View this thread: http://www.excelforum.com/showthread...hreadid=495102

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default easy way to check for forbidden characters?

Hi Ouka,

You could try something like:

Private Sub CommandButton1_Click()
Dim studyName As String
Dim testChar1 As String
Dim iLoop As Integer
Dim iChar As Integer
Dim bNameOk As Boolean

studyName = Trim(txbStudyName.Value)

bNameOk = True
For iLoop = 1 To Len(studyName)
testChar1 = Mid$(studyName, iLoop, 1)
iChar = Asc(testChar1)
If iChar = 46 Or iChar = 95 Then
' test for "." or "_"
ElseIf iChar = 48 And iChar <= 57 Then
' test for "0" upto "9"
ElseIf iChar = 65 And iChar <= 90 Then
' test for "A" upto "Z"
ElseIf iChar = 97 And iChar <= 122 Then
' test for "a' unto "z"
Else
bNameOk = False
Exit For
End If
Next

If bNameOk < "" Then
MsgBox "Study name is invalid."
Else
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End If
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default easy way to check for forbidden characters?


Thanks, that set me on the right path. Made a couple of alterations and
it works like a charm. Totally forgot about the asci number ranges
*duh*

So, anyone know if there is a way to do the second part of my question?
To make VBA code check for the existance of identical file names in the
target windows folder before the saveas routine fires off and excel
itself returns the error?


--
Ouka
------------------------------------------------------------------------
Ouka's Profile: http://www.excelforum.com/member.php...o&userid=23988
View this thread: http://www.excelforum.com/showthread...hreadid=495102

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
Site for some reason, and forbidden alagmy Excel Worksheet Functions 0 May 6th 10 10:22 PM
how to check first 5 characters of a cell & then sum jonny Excel Worksheet Functions 5 January 20th 09 01:41 PM
Finding Characters Conditionally - Easy Question Rothman Excel Worksheet Functions 4 August 30th 06 02:24 AM
Check for Alpha characters TimE Excel Discussion (Misc queries) 4 November 10th 05 12:31 AM
new user with easy question? not easy for me speakeztruth New Users to Excel 5 June 3rd 05 09:40 PM


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