In Column A I have a list of six digit numbers. I would like to find numbers that has a zero in the second and third digit in those numbers, and put a 1 for those numbers in the next column. Thank you.
In Column A I have a list of six digit numbers. I would like to find numbers that has a zero in the second and third digit in those numbers, and put a 1 for those numbers in the next column. Thank you.
This Code goes in a Standard Module. It adds a UserDefined Function to Excel's fx Icon
To use, Assume that the number with zeros is in Column "A1", place this formula in "B1"Option Explicit Public Function ZerosToOnes(source As Range) Dim Second As Boolean Dim Third As Boolean If Mid(CStr(source), 2, 1) = "0" Then Second = True If Mid(CStr(source), 3, 1) = "0" Then Third = True If Second And Third Then ZerosToOnes = CDbl(Replace(CStr(source), "0", "1", 1, 2, 1)) Exit Function End If ZerosToOnes = "" End FunctionYou can treat UDFs like any other Excel Function=ZerosToOnes(A1)
Edited: I liked Pauls code below so I shortening my code. It still works like the original
Public Function ZerosToOnes(source As Range) If Mid(CStr(source), 2, 2) = "00" Then ZerosToOnes = CDbl(Replace(CStr(source), "0", "1", 1, 2, 1)) 'To leave a blank cell as you didn't specify Else: ZerosToOnes = "" 'Or use To not leave a blank 'Else: ZerosToOnes = source End If End Function
Last edited by SamT; 05-06-2018 at 11:17 AM.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
Not much error checking
Option Explicit Sub ZerosToOnes() Dim r As Range, r1 As Range With ActiveSheet Set r = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) End With For Each r1 In r.Cells If Len(r1.Text) = 6 Then If Mid(r1.Text, 2, 2) = "00" Then r1.Offset(0,1).Value = Left(r1.Text, 1) & "11" & Right(r1.Text, 3) End If End If Next End Sub
Last edited by Paul_Hossler; 05-06-2018 at 11:49 AM. Reason: removed extra dot
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3
Thanks for your help. Code works great. I really like the shorter one.