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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code Not working 2

Status
Not open for further replies.

rekhatr10

Programmer
Apr 2, 2004
67
US
Hi everyone,

Below is my code to go and pick all the even number from right. But I am not able to do it .Bar
TotalOdd will be 57
But I want to get 75. Could you look at the code & help me figure out where I should modify my code. I tried right function but not able to get it.
Any suggestions will be helpful.
Thank you

Dim i As Integer
Dim TotalOdd As String
Dim Total As Integer
Dim Barcode As String
Dim checkdigit As Integer

Barcode = 4567
'get odd numbers
MsgBox Len(Barcode)

For i = 1 To Len(Barcode)
i = i - 1
TotalOdd = TotalOdd + (Mid(Barcode, i, 1))
MsgBox TotalOdd
Next i


 
Maybe this would suit:
Code:
    Dim i As Integer
    Dim TotalOdd As String
    Dim Total As Integer
    Dim Barcode As String
    Dim checkdigit As Integer
  
    Barcode = 4567911
    'get odd numbers
    Debug.Print Len(Barcode)

    For i = 1 To Len(Barcode)
      'i = i - 1
      If Round(Mid(Barcode, i, 1) / 2, 0) <> Mid(Barcode, i, 1) / 2 Then
        TotalOdd = TotalOdd + (Mid(Barcode, i, 1))
        Debug.Print TotalOdd
      End If
    Next i
 
Good code Remou, but I'm under the impression,
rehhatr10, wanted the result you got, inverted.
eg; 11975, not 57911

If you don't mind, i'm going to slightly modify your code,
Dim i As Integer
Dim TotalOdd As String
Dim Total As Integer
Dim Barcode As String
Dim checkdigit As Integer

Barcode = 4567911
'get odd numbers
Debug.Print Len(Barcode)

i = Len(Barcode)

Do Until i = 1
If Mid(Barcode, i, 1) Mod 2 <> 0 Then
TotalOdd = TotalOdd + (Mid(Barcode, i, 1))
Debug.Print TotalOdd
End If
i = i - 1
Loop
 
Zion & Remou,

Thank you both for helping me. It worked like a charm. Thank you both again for helping me

 
Just a suggestion:
Code:
    For i = Len(Barcode) to 1 Step -1
      If Mid(Barcode, i, 1) Mod 2 <> 0 Then
        TotalOdd = TotalOdd + (Mid(Barcode, i, 1))
        Debug.Print TotalOdd
      End If
    Next
 
Why not simply this ?
Barcode = 4567911: TotalOdd = ""
For i = 1 To Len(Barcode)
j = Mid(Barcode, i, 1)
TotalOdd = IIf(j Mod 2, j, "") & TotalOdd
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank-you everyone for the stars.

But personally, as you all may agree,
I do Like PHV's the best, then ddiamond's.

...although, with a little more time,
I'm sure I would've come to the same conclusion...LOL!
 
Zion, PHV, Diamond

How would you hadle a zero inside the string. Say 3250027
the code completely skips as it is equal to 0

Rekha
 
Something like this ?
Barcode = 3250027: TotalOdd = ""
For i = 1 To Len(Barcode)
j = Mid(Barcode, i, 1)
TotalOdd = IIf(j Like "[013579]", j, "") & TotalOdd
Next
Result: 70053

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Diamond
3250027

Zero will be in odd position althought it is an even number. I am picking up numbers from the odd postions from right.
 
This is about check digits, so it is not odd numbers, it is odd positions yes no? So it is back to dddiamonds idea, with a small modification:
Code:
    For i = Len(Barcode) to 1 Step -1

      If i Mod 2 <> 0 Then
        TotalOdd = TotalOdd + (Mid(Barcode, i, 1))
        Debug.Print TotalOdd
      End If
    Next

(not tested)
 
Remou,

Thank you it works, don't know why I didn't realize that.

 
So, you wanted this ?
For i = Len(Barcode) to 1 Step -2
TotalOdd = TotalOdd & Mid(Barcode, i, 1)
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You may wish to do the multiplying etc within the loop:
Code:
For i = Len(Barcode) to 1 Step -1

  If i Mod 2 <> 0 Then
    CheckDigit = CheckDigit + Mid(Barcode, i, 1) * Whatever
  Else
    CheckDigit = CheckDigit + Mid(Barcode, i, 1) * WhateverElse
  End If
Next
I hope it is not Mod 10, it is not the best on 3 and 8, as far as I recall.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top