Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default Cleaning up code

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default Cleaning up code

I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Cleaning up code

Your second attempt is much better than your first. Always avoid goto's
except to invoke an error handling routine. Here is my take...

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long

Do Until tryagain = vbNo
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
With shtInput
.Range("State_Code") = InpType
.Range("product") = ""
End With
tryagain = vbNo
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub

It is not much different. I removed the 7 and replaced it with vbNo.
Additionally I added a with statement as that makes the code marginally more
efficient.
--
HTH...

Jim Thomlinson


"Brad" wrote:

I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default Cleaning up code

Thank you (again)!

When I first started, I didn't know how to structure it. I try to avoid
"ends" and GOTO's as much as possible. But sometimes it is easier to change
something that works than coming up with the original idea....


"Jim Thomlinson" wrote:

Your second attempt is much better than your first. Always avoid goto's
except to invoke an error handling routine. Here is my take...

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long

Do Until tryagain = vbNo
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
With shtInput
.Range("State_Code") = InpType
.Range("product") = ""
End With
tryagain = vbNo
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub

It is not much different. I removed the 7 and replaced it with vbNo.
Additionally I added a with statement as that makes the code marginally more
efficient.
--
HTH...

Jim Thomlinson


"Brad" wrote:

I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cleaning up code

This is probably how I would have written it...

Sub xxCodes()
Dim InpType As String
Dim shtInput As String
Do
InpType = InputBox("Enter the school code")
If InpType Like "[xX][xX][1-5]" Then
shtInput.Range("State_Code") = UCase(InpType)
shtInput.Range("product") = ""
Exit Do
End If
Loop While vbYes = MsgBox("Incorrect school code do you " & _
"want to try again?", vbYesNo)
End Sub

I do have one question though... your are referencing a worksheet variable
shtInput, but I don't see where you have set it at... are you doing it
through some kind of global setting or did you just forget to include it in
your code?

Rick

"Brad" wrote in message
...
I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cleaning up code

Damn... I meant to leave the Dim shtInput statement out until you answered
the question (if it is set globally, the Dim statement isn't necessary)...

Sub xxCodes()
Dim InpType As String
Do
InpType = InputBox("Enter the school code")
If InpType Like "[xX][xX][1-5]" Then
shtInput.Range("State_Code") = UCase(InpType)
shtInput.Range("product") = ""
Exit Do
End If
Loop While vbYes = MsgBox("Incorrect school code do you " & _
"want to try again?", vbYesNo)
End Sub


Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
This is probably how I would have written it...

Sub xxCodes()
Dim InpType As String
Dim shtInput As String
Do
InpType = InputBox("Enter the school code")
If InpType Like "[xX][xX][1-5]" Then
shtInput.Range("State_Code") = UCase(InpType)
shtInput.Range("product") = ""
Exit Do
End If
Loop While vbYes = MsgBox("Incorrect school code do you " & _
"want to try again?", vbYesNo)
End Sub

I do have one question though... your are referencing a worksheet variable
shtInput, but I don't see where you have set it at... are you doing it
through some kind of global setting or did you just forget to include it
in your code?

Rick

"Brad" wrote in message
...
I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default Cleaning up code

Thanks!

I'm going to have to look up what

If InpType Like "[xX][xX][1-5]" Then
is really doing - I don't see why the [xX] has to be repeated twice. If you
could shed some light I would appreciate it.

my second dim statement was
Dim tryagain As Long
not
Dim shtInput As String - whch you have - don't know were you got this?
--
Wag more, bark less


"Rick Rothstein (MVP - VB)" wrote:

This is probably how I would have written it...

Sub xxCodes()
Dim InpType As String
Dim shtInput As String
Do
InpType = InputBox("Enter the school code")
If InpType Like "[xX][xX][1-5]" Then
shtInput.Range("State_Code") = UCase(InpType)
shtInput.Range("product") = ""
Exit Do
End If
Loop While vbYes = MsgBox("Incorrect school code do you " & _
"want to try again?", vbYesNo)
End Sub

I do have one question though... your are referencing a worksheet variable
shtInput, but I don't see where you have set it at... are you doing it
through some kind of global setting or did you just forget to include it in
your code?

Rick

"Brad" wrote in message
...
I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cleaning up code

Think of the Like operator as a miniature version of a Regular Expression
parser... it works by comparing text strings to text "patterns". The
asterisk (0 or more characters) and question mark (a single character) are
pretty well-known wildcards and Like makes use of them. In addition, you can
specify multiple characters for any given character position. That is what
the square brackets do... signify a the characters that may appear at the
given position within the pattern string. In my code's case, the choice of
characters is between an lower case X and an upper case X (that is why it
appears twice, once in lower case and once in upper case). So in my code,
the InpType variable is being checked to see if the first two characters are
either a lower case X or an upper case X (that is why [xX] appears twice,
once for each character position) followed by one of the digits 1 through 5.
You should check out the Like operator in the help files... it is a power
built-in tools that comes in quite handy in many situations.

Rick


"Brad" wrote in message
...
Thanks!

I'm going to have to look up what

If InpType Like "[xX][xX][1-5]" Then
is really doing - I don't see why the [xX] has to be repeated twice. If
you
could shed some light I would appreciate it.

my second dim statement was
Dim tryagain As Long
not
Dim shtInput As String - whch you have - don't know were you got this?
--
Wag more, bark less


"Rick Rothstein (MVP - VB)" wrote:

This is probably how I would have written it...

Sub xxCodes()
Dim InpType As String
Dim shtInput As String
Do
InpType = InputBox("Enter the school code")
If InpType Like "[xX][xX][1-5]" Then
shtInput.Range("State_Code") = UCase(InpType)
shtInput.Range("product") = ""
Exit Do
End If
Loop While vbYes = MsgBox("Incorrect school code do you " & _
"want to try again?", vbYesNo)
End Sub

I do have one question though... your are referencing a worksheet
variable
shtInput, but I don't see where you have set it at... are you doing it
through some kind of global setting or did you just forget to include it
in
your code?

Rick

"Brad" wrote in message
...
I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default Cleaning up code

Read about Like - "like" it.

Thank you very much.


"Rick Rothstein (MVP - VB)" wrote:

This is probably how I would have written it...

Sub xxCodes()
Dim InpType As String
Dim shtInput As String
Do
InpType = InputBox("Enter the school code")
If InpType Like "[xX][xX][1-5]" Then
shtInput.Range("State_Code") = UCase(InpType)
shtInput.Range("product") = ""
Exit Do
End If
Loop While vbYes = MsgBox("Incorrect school code do you " & _
"want to try again?", vbYesNo)
End Sub

I do have one question though... your are referencing a worksheet variable
shtInput, but I don't see where you have set it at... are you doing it
through some kind of global setting or did you just forget to include it in
your code?

Rick

"Brad" wrote in message
...
I think this is better - suggestions are welcome

Sub xxlCodes1()
Dim InpType As String
Dim tryagain As Long
tryagain = 6
Do Until tryagain = 7
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "xx1" To "xx5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
tryagain = 7
Case Else
tryagain = MsgBox("Incorrect school code do you want to try
again?", vbYesNo)
End Select
Loop
End Sub



"Brad" wrote:

The following code works but I think could be improved.

Any suggestions would be appreciated!

Sub xxCodes()
Dim InpType As String
Dim tryagain As Long
startinput:
InpType = UCase(InputBox("Enter the school code"))
Select Case InpType
Case "XX1" To "XX5"
shtInput.Range("State_Code") = InpType
shtInput.Range("product") = ""
Case Else
tryagain = MsgBox("Do you want to try again", vbYesNo)
If tryagain = 6 Then
GoTo startinput
Else
End
End If
End Select
End Sub



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
Cleaning up some code? Patrick[_14_] Excel Programming 3 August 16th 06 12:02 AM
Help Cleaning up Code Lost and Looking for Help Excel Programming 1 May 24th 06 02:26 PM
Need help Cleaning up Code Joel Mills Excel Programming 5 December 16th 04 10:36 PM
Help with shortening/cleaning some code please roy Excel Programming 3 June 3rd 04 11:49 PM
VBA Code -- Cleaning Data Craig[_8_] Excel Programming 2 December 12th 03 12:21 AM


All times are GMT +1. The time now is 02:49 AM.

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"