Solved

VB6 For Loop

Posted on 2007-03-22
6
37,982 Views
Last Modified: 2008-02-01
I have a small routeen that removes all characters from a string but not any digit 0 thru 9. It works fine, but I was wondering if my For Next loop could be modified to be cleaner. Here is my code:

'*********************************************

For i = 0 To 47
MyString = Replace(MyString, Chr(i), "")
Next i

For i = 58 To 127
MyString = Replace(MyString, Chr(i), "")
Next i

'********************************************************
0
Comment
Question by:AmericaFan
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 18773570
For i = 0 To 47
  MyString = Replace(MyString, Chr(i), "")
Next i

1. Do you really need to concern yourself with characters less than 32?

2. Is cleaner really your problem, or are you experiencing performance problems?  Your two loops look pretty clean.  You combine these two loops, but wouldn't gain any simplicity or cleanliness.

3. How long are your MyString strings?
0
 

Author Comment

by:AmericaFan
ID: 18773708
My main goal was to see if I could combine the two For Loops into one For Loop and get the same results. I only want 0-9 and any other character has to be removed. I'm validating and any key is possible. Maybe not 0 thru 31, but the rest is not wanted and is on a key board for mistakes. Thanks for you comment aikimark.
0
 
LVL 10

Accepted Solution

by:
Clif earned 50 total points
ID: 18773750
For i = 58 To 127
    MyString = Replace(MyString, Chr(i), "")
    If (i - 58) <= 47 Then
        MyString = Replace(MyString, Chr(i - 58), "")
    End If
Next i
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Expert Comment

by:Clif
ID: 18773997
You can also use regular expressions, which is even more efficient.

You would have to add a reference to "Microsoft VBScript Regular Expressions 5.5" in your project (it's been available since IE 5.5)

0
 

Author Comment

by:AmericaFan
ID: 18774165
Thats what I'm talkin about :) Thanks Clif!
0
 
LVL 10

Expert Comment

by:Clif
ID: 18774253
NP
Thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now