Delte Text from a column

TayoMayo

New Member
Joined
Jul 24, 2014
Messages
22
Hey everyone,
I am pretty new to VBA and I am wondering if there is a way to delete text from a colum that has both numbers and letters in it. For example, if the column is one that is holding weight values, I would need to get rid of all the lbs, LBS, lbs., etc from the column and leave only the text. Is there a way to do this? I don't have any code to post, because I have no idea how to go about starting. Thank you for any and all replies.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Why not a CSE formula?

E.g.

526 lbs526
526 LBS526
526Lbs526
526 lbs.526
526 LBS.526
526lbs526
148 LBS148
148 lbs.148
148 lb148
469lb.469
469lb.469
148lb148

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>

{=1*MID(A1,MATCH(TRUE,ISNUMBER(1*MID(A1,ROW($1:$12),1)),0),COUNT(1*MID(A1,ROW($1:$12),1)))}
 
Upvote 0
Mr. Rothstein,
Is there a way to modify your code to also do a conversion? So for the data I posted earlier, if there was a kg measurement in there, is it possible for it to convert it to lbs, then delete the kg text from the cell? So at the end, there would be a column of only numbers that is all in pound measurements, even if it was originally in kilograms.
I assume from your posted data that values in Kilograms appear as numbers only (without any text in the cell with them). If so, just adding the line I show in red should do what you want...
Code:
Sub LeaveNumbersOnly()
  Dim LastRow As Long, UnusedCol As Long
  Const StartRow As Long = 1
  Const ColNum As Long = 3
  Application.ScreenUpdating = False
  LastRow = Cells(Rows.Count, ColNum).End(xlUp).Row
  UnusedCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
  With Cells(StartRow, ColNum).Resize(LastRow - StartRow + 1)
[COLOR=#FF0000][B]    .Value = Evaluate(Replace("IF(ISNUMBER(C1:C#),2.2046226*C1:C#,C1:C#)", "#", LastRow))[/B][/COLOR]
    .Offset(, UnusedCol - ColNum).FormulaR1C1 = "=LOOKUP(9.9E+307,--LEFT(RC" & ColNum & ",ROW(R1:R99)))"
    .Value = .Offset(, UnusedCol - ColNum).Value
  End With
  Columns(UnusedCol).Clear
  Application.ScreenUpdating = True
End Sub
[COLOR=#FF0000][/COLOR]
 
Upvote 0
Hiker95,
Yes I used your macros and they worked very well. I just was looking to modify one of the codes that were posted and thought it might be easier to modify the other because it looked simpler.

Mr. Rothstein,
If the data still has the kg text in the cells, how would the code change? So if the data looked like what is posted below, how would be code be effected? I am trying to understand how all of this is working so that hopefully I will be able to construct my own version of it next time a problem like this arises.

526 lbs
526 kg
526Lbs
526 lbs.
526 KG
526lbs
148 LBS
148 kg.
148 kg
469lb.
469kg
148lb


<tbody>
</tbody>
 
Upvote 0
TayoMayo,

New sample raw data:


Excel 2007
C
1526 lbs
2526 kg
3526Lbs
4526 lbs.
5526 KG
6526lbs
7148 LBS
8148 kg.
9148 kg
10469lb.
11469kg
12148lb
13
Sheet1


After the new macro:


Excel 2007
C
1526
21159.631488
3526
4526
51159.631488
6526
7148
8326.2841448
9326.2841448
10469
111033.967999
12148
13
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Compare Text
Sub ExtractNumbers_V3()
' hiker95 , 07/30/2014, ME794797
Dim c As Range
Application.ScreenUpdating = False
Dim RE As RegExp
Set RE = New RegExp
RE.Pattern = "\D"
RE.Global = True
For Each c In Range("C1", Range("C" & Rows.Count).End(xlUp))
  If InStr(c, "lb") Then
    c.Value = RE.Replace(c.Text, "")
  ElseIf InStr(c, "kg") Then
    c.Value = RE.Replace(c.Text, "") * 2.2046226
  End If
Next c
Columns(3).AutoFit
Application.ScreenUpdating = True
End Sub

You may have to add the Microsoft VBScript Regular Expressions 1.0 to the References - VBA Project.

With your workbook that contains the above:

Press the keys ALT + F11 to open the Visual Basic Editor

In the VBA Editor, click on:
Tools
References...

Put a checkmark in the box marked
Microsoft VBScript Regular Expressions 1.0

Then click on the OK button.

And, exit out of the VBA Editor.

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ExtractNumbers_V3 macro.


What type of formatting would you like (number of decimal places) for the cells that contained kg?
 
Last edited:
Upvote 0
Thank you very much for the code, it works wonderfully. I am to concerned with the number of decimal places at this point. I really only need 1 decimal place, and I was going to format the cells in excel to only show 1 decimal. If that didnt work then I was just going to modify the multiplier that is in the code.
 
Upvote 0
How would I change the column that was being processed? The code works for the sample data that posted perfectly, but for the larger amount of data that I am working with, it will need to be applied to different columns.
 
Upvote 0
Mr. Rothstein,
If the data still has the kg text in the cells, how would the code change? So if the data looked like what is posted below, how would be code be effected? I am trying to understand how all of this is working so that hopefully I will be able to construct my own version of it next time a problem like this arises.

526 lbs
526 kg
526Lbs
526 lbs.
526 KG
526lbs
148 LBS
148 kg.
148 kg
469lb.
469kg
148lb

<tbody>
</tbody>
Just to keep this thread update, here is the modification to my code which will handle cells with or without "kg" in them as kilograms and convert them to pounds...

Rich (BB code):
Sub LeaveNumbersOnly()
  Dim LastRow As Long, UnusedCol As Long, Addr As String
  Const StartRow As Long = 1
  Const ColNum As Long = 3
  Application.ScreenUpdating = False
  LastRow = Cells(Rows.Count, ColNum).End(xlUp).Row
  UnusedCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
  Addr = Cells(1, ColNum).Resize(LastRow).Address
  With Cells(StartRow, ColNum).Resize(LastRow - StartRow + 1)
    .Value = Evaluate(Replace("IF(ISNUMBER(@),2.2046226*@,IFERROR(2.2046226*LEFT(@,SEARCH(""kg"",@)-1),@))", "@", Addr))
    .Offset(, UnusedCol - ColNum).FormulaR1C1 = "=LOOKUP(9.9E+307,--LEFT(RC" & ColNum & ",ROW(R1:R99)))"
    .Value = .Offset(, UnusedCol - ColNum).Value
  End With
  Columns(UnusedCol).Clear
  Application.ScreenUpdating = True
End Sub
If you want to apply this code to other columns, simply change the column number being assigned to the ColNum constant in the highlighted Const statement.
 
Upvote 0
TayoMayo,

After the newest macro:


Excel 2007
C
1526.0
21159.6
3526.0
4526.0
51159.6
6526.0
7148.0
8326.3
9326.3
10469.0
111034.0
12148.0
13
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Compare Text
Sub ExtractNumbers_V4()
' hiker95 , 07/30/2014, ME794797
Dim c As Range
Application.ScreenUpdating = False
Dim RE As RegExp
Set RE = New RegExp
RE.Pattern = "\D"
RE.Global = True
For Each c In Range("C1", Range("C" & Rows.Count).End(xlUp))
  If InStr(c, "lb") Then
    c.Value = RE.Replace(c.Text, "")
  ElseIf InStr(c, "kg") Then
    c.Value = RE.Replace(c.Text, "") * 2.2046226
  End If
Next c
Range("C1", Range("C" & Rows.Count).End(xlUp)).NumberFormat = "0.0"
Columns(3).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ExtractNumbers_V4 macro.



If you get an error concerning the next line of code, then, in the module where you have the macro, delete one of the two, following lines of code:

Code:
Option Compare Text
 
Upvote 0
Mr. Rothstein,
Is it possible to change it slightly so that it will only convert the cells that contain "kg" in them? If there is a cell without either kg or lbs, then it is already in pounds. I apologize that it is taking so much time and effort to try and get this right.
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,656
Members
449,114
Latest member
aides

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