Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Then Else Routine problems

Status
Not open for further replies.

Schnappa

Technical User
Jul 27, 2003
58
AU
Hi all

The following code works except that it defaults every time to the else clause - or seems to. The First part of the if clause should capture any Race Number (using count) that is less than ten and add a zero preceeding the race number in the HTML statement. Currently, evertime I run the code, I do not get a zero - every line seems to have picked up the else clause. This seems very illogical as the count starts from one, so it should automatiaccly default to the first part of the clause.

Here is the code in question. The input sheet is basically

C3 = PT
C4 = 8

D3 = PG
D4 = 12



Sub MainInput()
Dim Count As Integer
Dim PoVal As String
Dim Cter As Integer
Cter = 1
Call CleanSheets
Do Until IsEmpty(ActiveCell)
PoVal = ActiveCell.Offset(-1, 0)
For Count = 1 To (ActiveCell.Value)
If Count < 10 Then Worksheets("HTML Creator").Cells(Cter, "A") = _
" & _
Worksheets("Race Details Entry").Range("$C$17").Value & "/" & _
Worksheets("Race Details Entry").Range("$D$17").Value & "/" & _
Worksheets("Race Details Entry").Range("$E$17").Value & "/" & _
PoVal & "0" & Count & ".html" Else
Worksheets("HTML Creator").Cells(Cter, "A") = _
" & _
Worksheets("Race Details Entry").Range("$C$17").Value & "/" & _
Worksheets("Race Details Entry").Range("$D$17").Value & "/" & _
Worksheets("Race Details Entry").Range("$E$17").Value & "/" & _
PoVal & Count & ".html"
Sheets("HTML Creator").Select
ActiveCell.Offset(2, 0).Select
Cter = Cter + 2
Next Count
Sheets("Race Details Entry").Select
ActiveCell.Offset(0, 1).Select
Loop
End Sub

I have tried to heaps of things to get it to work, to no avail - including adding another variable to count the race numbers seperately to the count integer. Nothing seems to work for me. The webiste to resolve needs two digits - thus my problem

Thanks for any help you might be able to give

Cheers

GV
 




Hi
I would strongly suggest avoiding the use of the Select and Activate methods. Rather explicitly reference ranges.
Code:
    lRow = 2
    Do Until (sheets("Sheet1").cells(lrow, "A").value="")

       lrow = lrow + 1
    Loop
It's not a good practice to change the loop limit values during the execution of a loop. Leave the limit values and rather change a criteria within the loop to allow an exit, shuch as...
Code:
iLimit = somevalue
for i = 0 to iLimit


  if testvalue > othervalue then [b]exit for[/b]
next


Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
Thanks for that Skip

Having only started writing VBA code on Monday, I am a bit confused by the meaning of your post. If you could explain what it is you mean, I can take it from there.

Thanks

GV
 
Besides what Skip says, you could avoid the IF block by modifying it to

Worksheets("HTML Creator").Cells(Cter, "A") = _
" & _
Worksheets("Race Details Entry").Range("C17").Value & "/" & _
Worksheets("Race Details Entry").Range("D17").Value & "/" & _
Worksheets("Race Details Entry").Range("E17").Value & "/" & _ PoVal & right("0" & Count, 2) & ".html"
 
Try this syntax:
If condition Then
instructions
Else
instructions
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 



Select & Activate slow code execution & make it difficult to debug code.

In many languages, once you set loop limits, the limts are frozen. It is generally a more acccepted coding approch to set limits, leave them alone and, if necessary, test for exit condition(s) within the loop, using, an Exit statement.
VB_Help said:
Tip Changing the value of counter while inside a loop can make it more difficult to read and debug your code.
If you have specific question, feel free to ask.


Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
Why not remove the if then and just use

format(count,"00")

" & _
Worksheets("Race Details Entry").Range("$C$17").Value & "/" & _
Worksheets("Race Details Entry").Range("$D$17").Value & "/" & _
Worksheets("Race Details Entry").Range("$E$17").Value & "/" & _
PoVal & format(count,"00") & ".html"


ck1999
 
Thanks to everyone

I will take some time to digest these suggestions - I appreciate your help.

Cheers

GV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top