Preface a number with '

Trebormac

New Member
Joined
Sep 10, 2009
Messages
32
I have 2 excel files with columns of serial numbers in each file. The numbers are formatted as numbers in one file and as text with a leading ' in the other file. I need a macro to add the ' to the cells formatted as numbers in order to do a Vlookup.

Thanks for your help!

Trebormac
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Just FYI, you can do this without a macro. Create a helper column, type this formula (assuming the number is in A2 and down):

=TEXT(A2, "0")

Then copy/paste values on top of your existing numbers.
 
Upvote 0
You're welcome... does that work for you, or do you actually need a macro?
 
Upvote 0
Strange ... worked for me.

Let's try the macro way. To start, select the cells where you want to insert ' before the number, then run this macro:

Code:
Public Sub InsertApostrophe()
  Dim rng As Excel.Range
  Dim rngSel As Excel.Range
  Dim s As String
  
  Dim calcs As Excel.XlCalculation
  
  calcs = Application.Calculation
  Application.Calculation = xlCalculationManual
  
  On Error GoTo cleanup
  
  If StrComp(LCase(TypeName(Selection)), "range") <> 0 Then
    Err.Raise 12536, "InsertApostrophe()", "No cell range selected. Operation not valid."
  End If
  
  Set rngSel = Selection
  
  For Each rng In rngSel.Cells
    On Error Resume Next
      s = CStr(rng.Value)
    On Error GoTo cleanup
    If StrComp(Left$(s, 1), "'") <> 0 And IsNumeric(s) Then
      s = "'" & s
      rng.Value = s
    End If
  Next rng
  
cleanup:
  Application.Calculation = calcs
  If Err.Number <> 0 Then
    Call MsgBox("An error occured." & vbCrLf & Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation, Err.Source)
  End If
End Sub

Post back with the result.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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