VBA code to Add "00" to any cell in a column that begins with an 8

jhgiants95

New Member
Joined
Jul 21, 2014
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I am looking for VBA code to help me add "00" to any cell in a column that begins with an 8. This will allow me to complete a macro that I am working on. Thanks.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Since you did not say what column. You will have to put the column letter into the Inputbox
And you said add to so I assumed you meant add after.
so if value in cell is 8129 it will now say 812900
Or if value in cell is 8Apples it will now say 8Apples00

Try this:
Code:
Sub Add_Zero()
'Modified 1-31-18 7:50 PM EST
Application.ScreenUpdating = False
Dim ans As String
Dim c As Range
ans = InputBox("What column? Enter column like this", "Enter Value like this C", "C")
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, ans).End(xlUp).Row
For Each c In Range(Cells(1, ans), Cells(Lastrow, ans))
    If Left(c, 1) = "8" Then c.Value = c.Value & "00"
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for your response. I apologize for leaving out some details in my initial post. I would like the "00" to be added at the beginning of the cell actually, but I fixed this myself by switching the c.value with the "00". Is there any way that this can be done without the input box, let's assume that the column this will occur in is column "P" every time. I am new to VBA and am trying to learn how to automate certain things that I am involved with daily. Thanks, other than this its works as needed.
 
Upvote 0
Try this:
Code:
Sub Add_Zero()
'Modified 1-31-18 8:25 PM EST
Application.ScreenUpdating = False
Dim c As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "P").End(xlUp).Row
For Each c In Range(Cells(1, "P"), Cells(Lastrow, "P"))
    If Left(c, 1) = "8" Then c.Value = "00" & c.Value
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here is another macro that you can also consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub Add_Zero()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "P").End(xlUp).Row
  Range("P1:P" & LastRow) = Evaluate(Replace("IF(LEFT(P1:P#)=""8"",""008""&MID(P1:P#,2,99),IF(P1:P#="""","""",P1:P#))", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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