Look For 12 Digit Number & Do Some Calculations

dgr

Board Regular
Joined
Apr 24, 2005
Messages
176
Hello,
I'm using Excel 2013. I'm looking for a vb macro solution.

I want to find all 12 digit numbers in column C. If this 12 digit number is an odd number, then I would like to enter the number "2" in column E on the same row as the 12 digit digit number. If this 12 digit number is an even number, then I would like to enter the number "1" in column E on the same row as the 12 digit digit number.

For example:

<b>Column C</b> ........................... <b>Column E</b>
<font color="blue">860512125211 ..................... 2
86051212 ............................ ignore as this is not a 12 digit number
850524085748 ..................... 1
860407435242 ..................... 1
hello ................................. ignore as this is not a 12 digit number</font>

Could you show me the code to achieve this please?

Thanks a lot.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Code:
Sub Look_For_12_Digits()
Dim A
With Range("c1:c" & Range("c" & Rows.Count).End(xlUp).Row)
    A = Evaluate(Replace$("=IF(ISNUMBER(@)*(LEN(@)=12),IF(MOD(@,2),2,1),"""")", "@", .Address))
    .Offset(, 2).Value = A
End With
End Sub
 
Upvote 0
Thanks for your reply VBA Geek. All I got was #NUM! in column E. Why is that so?
 
Upvote 0
Thanks for your reply, MrExcel MVP. Sorry, I'm looking for a vb macro solution. Nevertheless, I tried your formula. The cell in which I pasted the formula is blank.
 
Upvote 0
You can use Jonmo's formula in code.
Code:
Sub Odd_Or_Even()
    With Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row).Offset(, 2)
        .Formula = "=IF(LEN(RC[-2])=12,ISODD(RC[-2])+1,"""")"
        .Value = .Value
    End With
End Sub
 
Upvote 0
You can use Jonmo's formula in code.
Code:
Sub Odd_Or_Even()
    With Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row).Offset(, 2)
        .Formula = "=IF(LEN(RC[-2])=12,ISODD(RC[-2])+1,"""")"
        .Value = .Value
    End With
End Sub

Thanks a lot jolivanes. This worked well. I appreciate your help. Have a blessed day :)
 
Upvote 0
Glad it worked for you.
Thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,215,262
Messages
6,123,950
Members
449,134
Latest member
NickWBA

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