Remove leading Quote mark from string e.g '1-4 to get only 1-4

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello,

This has turned out to be hard, I have searched and searched on how to do this but found nothing that works

I have data that comes from a survey app, in column V that there is data like this

Code:
Years of Service
1-4
5-9
10-14
15-20
20-24
.
.
.

all the strings for 1-4, 5-9, 10-14 are being read by Excell as Dates and being turned into numbers 42739, 42864, 43022

So I first with vba FindReplace
Code:
[{"42739", "42864", "43022"}] >>  [{"'42739", "'42864", "'43022"}]  'I add leading single qoute mark

Then I format the Column with vba
Code:
rng.NumberFormat = "@"

Then with vba FindReplace
Code:
[{"'42739", "'42864", "'43022"}]  >>  [{"1-4", "5-9", "10-14"}]

I need this final step in vba
Code:
[{"'1-4", "'5-9", "'10-14"}]   >>   [{"1-4", "5-9", "10-14"}]  'Remove the leading single quote mark

BUT I can find NOTHING that will do this

Thanks for any help on this
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Steve, thank you for the response. It is coming from a CSV file, so the problem already exists by the time I get it.

Marcelo, thank you for the response. That reversed all the entries e.g. '1-4 became '4-1
 
Last edited:
Upvote 0
See if this formula corrects everything. Its not easy for me to test as im using UK dates:

=IF(ISNUMBER(A1),IF(YEAR(A1)=YEAR(TODAY()),TEXT(A1,"m-d"),TEXT(A1,"m-yy")),A1)

Then we can convert to VBA if you require.
 
Upvote 0
Steve, thanks again.

The formula works:) if I add it manually, but I need a VBA solution, so I tried

Code:
Sub test()
    Dim LRow As Long
    Dim ws As Worksheet


    Set ws = Sheets("Z")


    With ws
    LRow = ws.Range("A" & .Rows.Count).End(xlUp).Row


       With .Range("K1:K" & LRow)
            .Formula = "=IF(ISNUMBER(A1),IF(YEAR(A1)=YEAR(TODAY()),TEXT(A1,""m-d""),TEXT(A1,""m-yy"")),A1)"
            .value = .value
       End With
    End With
End Sub

But this gives the same problem in that the cells give date again

Is there a way to use your formula in vba that will work?

Thank you
 
Last edited:
Upvote 0
Give this code a try...
Code:
Sub RemoveApostrophes()
  Dim Data As Variant
  With Range("K1", Cells(Rows.Count, "K").End(xlUp))
    Data = .Value
    .Clear
    .NumberFormat = "@"
    .Value = Data
  End With
End Sub
 
Upvote 0
Rick,

I think I get it, you put the values into an Array then, cleared the range, formatted the range as "Text" then posted back to the range, and the ' was not copied to the array only the numerical value, is this correct?
 
Upvote 0
Rick, that worked!! not sure why that is working though, thank you
The code works by storing all the values in the cells in an VB array in memory (the values do not include the leading apostrophe because that is not part of the value in the cell, rather, it is a directive to Excel to treat what comes after it as text), completely clearing the cell of whatever is in it, formatted the cell as text so that new values remain as entered without Excel trying to interpret them), then placing all of the stored values back in the cells they came from.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,028
Members
452,542
Latest member
Bricklin

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