Add '1' infront of cells with 9 digits in a column

Russmeister57

New Member
Joined
Jun 5, 2015
Messages
13
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

I have been reviewing but cant quite find the answer that i need.
I need a macro to search column 'J' in 'Sheet1!' for each cell that contains 9 digits and for it to add the number 1 in front of all these cells that contain 9 digits.
Column J will have in it all numerical digits ranging from either 0, 9 or 10 digits in each cell.

Any help would be greatly appreciated.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Don't know how you want to trigger the code, but this should work
VBA Code:
Sub AddAOne()
Dim Lrow As Long, n As Long
Dim rng As Range

Lrow = Cells(rows.count, "J").End(xlUp).Row
For n = 2 To Lrow
    Set rng = Cells(n, 10)
    If Len(rng) = 9 Then rng = 1 & rng
Next
Set rng = Nothing

End Sub
You might need it to disable other events because that could trigger other events when cells change.
 
Upvote 0
Solution
Don't know how you want to trigger the code, but this should work
VBA Code:
Sub AddAOne()
Dim Lrow As Long, n As Long
Dim rng As Range

Lrow = Cells(rows.count, "J").End(xlUp).Row
For n = 2 To Lrow
    Set rng = Cells(n, 10)
    If Len(rng) = 9 Then rng = 1 & rng
Next
Set rng = Nothing

End Sub
You might need it to disable other events because that could trigger other events when cells change.
That is perfect, thank you so much
 
Upvote 0
Glad I could help. If you have a solution it's customary to mark it that post as the solution so that it comes off the list of threads that need attention. It also advises others that worked, which may help them in the future.
 
Upvote 0
One more:
VBA Code:
Sub AddOne()
    Dim R As Range, S As String
    
    With Worksheets("Sheet1")
        For Each R In .Range("J2", .Range("J" & .Rows.Count).End(xlUp))  'Assumes there is a header row
            S = CStr(Trim(R.Value))
            If VBA.IsNumeric(S) And Len(S) = 9 Then
                R.Value = "1" & S
            End If
        Next R
    End With
End Sub
 
Upvote 0
Column J will have in it all numerical digits ranging from either 0, 9 or 10 digits in each cell.
If that is the case then this might be another option that does them all at once rather than line-by-line.

VBA Code:
Sub NineToTenDigits()
  With Range("J2", Range("J" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(len(#)=9,1&#,if(#="""","""",#))", "#", .Address))
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,340
Members
451,637
Latest member
hvp2262

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