Amatour coder needs help cleaning up code

fredrerik84

Active Member
Joined
Feb 26, 2017
Messages
383
Hi guys most of my script has been created based on Q and A here and all the help I've received is much appreciated.
I have been able to create a script that does what i want, but my code is real messy. I was hoping someone have a better solution for this code:

Code:
            'Home and away teams
            hteam = capsf(HTMLRows(i).Cells(IIf(i > 0, 5, 4)).innerText)
            ateam = capsf(HTMLRows(i).Cells(IIf(i > 0, 9, 7)).innerText)
            
            'Fixing under x teams and w teams
            If InStr(ateam, "(") Then
               If InStr(ateam, "21") = 0 Then
                  If InStr(ateam, "20") = 0 Then
                     If InStr(ateam, "(w)") = 0 Then
                        If InStr(ateam, "19") = 0 Then
                           ateam = Left(ateam, (InStr(ateam, "(")) - 2)
                        End If
                     End If
                  End If
               End If
            End If
            
            
            If InStr(hteam, "(") And InStr(hteam, "21") = 0 Then
               If InStr(hteam, "20") = 0 Then
                  If InStr(hteam, "(w)") = 0 Then
                     If InStr(hteam, "19") = 0 Then
                        hteam = Left(hteam, (InStr(hteam, "(")) - 2)
                     End If
                  End If
               End If
            End If

Best regards
frederik
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Your always doing doing the next IF when it's true. So why not using AND argument like what you started in the second block?
Code:
            'Home and away teams
            hteam = capsf(HTMLRows(i).Cells(IIf(i > 0, 5, 4)).innerText)
            ateam = capsf(HTMLRows(i).Cells(IIf(i > 0, 9, 7)).innerText)
            
            'Fixing under x teams and w teams
            If (InStr(ateam, "(")) And (InStr(ateam, "21") = 0) And (InStr(ateam, "20") = 0) And (InStr(ateam, "(w)") = 0) And (InStr(ateam, "19") = 0) Then
                           ateam = Left(ateam, (InStr(ateam, "(")) - 2)
            End If
            
            If (InStr(hteam, "(")) And (InStr(hteam, "21") = 0) And (InStr(hteam, "20") = 0) And (InStr(hteam, "(w)") = 0) And (InStr(hteam, "19") = 0) Then
                        hteam = Left(hteam, (InStr(hteam, "(")) - 2)
            End If
 
Upvote 0
Hi thanks for your reply , but i believe it should be "OR" because its either 21 , 20, 19 , 18 or (w) if the string contains either of the code should be activated .

Guess your code is pretty easy to change ., also looks much more clean :)

Edit: loos like "or" is not correct ill try as you first suggested
 
Last edited:
Upvote 0
For the first "part" of both your if, you don't really test anything.

If you want to have "(" somewhere in your code, you should use this InStr(hteam, "(")<>0 as InStr return any number if he found it and 0 if not.

Not if you want your "if" to activate if either one of the "part" is true, then you will need to change your syntax for this.

Code:
If (InStr(hteam, "(")<>0) Or (InStr(hteam, "21") <> 0) Or (InStr(hteam, "20") <> 0) Or (InStr(hteam, "(w)") <> 0) Or (InStr(hteam, "19") <> 0) Then

Now your if will "activate" if one of the string is founded.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top