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.
 
Mr. Rothstein,
I have done exactly what your instructions said in multiple new books to try and get it to work, but it always returns the same output. Do I need to add the tools or something that hiker was refering to earlier in the post?
Did you remember to delete the code you had in the Sheet Module? On the file you sent me, I moved the good data you showed in Column E over to Column C (replacing the bad results you showed from a prior running of my code), then I deleted the code from the Sheet1 Module, inserted a general Module (as I described in Message #29) and ran the code... it produced the expected results in Column C without any errors. I do not know why doing that does not work for you since I cannot duplicate the problem you describe, using your own workbook, so I am at a loss as to what to tell you to do now.
 
Upvote 0

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.
TayoMayo,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


If you can send your workbook via e-mail, why can't you post your workbook on BOX, with sensitive data changed.


You can upload your workbook to Box Net,

sensitive data changed

mark the workbook for sharing

and provide us with a link to your workbook.
 
Upvote 0
TayoMayo,

Thanks for the workbook.

Sample raw data in worksheet Sheet1:


Excel 2007
C
1475
2347 lbs
353kg
44753kgs
54 lbs
6534 LBS
753 KG
8453 LB
945 KGS
1034 LB
1153
12453
1345
1434
1553
16
Sheet1


After the new macro with a new function:


Excel 2007
C
1475.0
2347.0
3116.8
410478.6
54.0
6534.0
7116.8
8453.0
999.2
1034.0
1153.0
12453.0
1345.0
1434.0
1553.0
16
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).

1. Copy the below macro code, and, function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Compare Text
Sub ExtractNumbers_V5()
' hiker95 , 08/01/2014, ME794797
Dim c As Range
Application.ScreenUpdating = False
With Sheets("Sheet1")
  For Each c In .Range("C1", .Range("C" & Rows.Count).End(xlUp))
    If InStr(c, "lb") Then
      c.Value = TextNumV5(c, 0)
    ElseIf InStr(c, "kg") Then
      c.Value = TextNumV5(c, 0) * 2.2046226
    End If
  Next c
  .Range("C1", .Range("C" & Rows.Count).End(xlUp)).NumberFormat = "0.0"
  .Columns(3).AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub
Function TextNumV5(ByVal txt As String, ByVal ref As Boolean) As String
' jindon
' =TextNum(A1,1)
' 1 for Text only, 0 for Numbers only
With CreateObject("VBScript.RegExp")
  .Pattern = IIf(ref = True, "\d+", "\D+")
  .Global = True
  TextNumV5 = .Replace(txt, "")
End With
End Function


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, and, function, with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the ExtractNumbers_V5 macro.
 
Upvote 0
hiker95,
Thank you very much. That code works perfectly. The only question I have now is if I needed to change the column that is being processed by this code, what do I need to modify?
 
Upvote 0
hiker95,

Can you do me a favor? Would you run the code I posted in Message #22 against the workbook you downloaded and tell me if it works for you or not ? Thanks.
 
Upvote 0
TayoMayo,

Thanks for the feedback.

You are very welcome. Glad I could help.


if I needed to change the column that is being processed by this code, what do I need to modify?


You would change the two C's in the following line of code to the correct column letter.

Code:
  For Each c In .Range("[B][SIZE=3]C[/SIZE][/B]1", .Range("[B][SIZE=3]C[/SIZE][/B]" & Rows.Count).End(xlUp))
 
Upvote 0
You would change the two C's in the following line of code to the correct column letter.

Code:
For Each c In .Range("[B][SIZE=3]C[/SIZE][/B]1", .Range("[B][SIZE=3]C[/SIZE][/B]" & Rows.Count).End(xlUp))
You would also change these lines too, correct?
Code:
  .Range("C1", .Range("C" & Rows.Count).End(xlUp)).NumberFormat = "0.0"
  .Columns(3).AutoFit
The same two C's and change the 3 as well.
 
Upvote 0
TayoMayo,

if I needed to change the column that is being processed by this code, what do I need to modify?

You would change the two C 's in the following line of code to the correct column letter.

Rich (BB code):
  For Each c In .Range("C1", .Range("C" & Rows.Count).End(xlUp))



And, in the next line of code, C = 3:

Rich (BB code):
  .Columns(3).AutoFit


Column A = 1
Column B = 2, and, so on....


If you need further help, let me know the new column, and, I will give you the correct number for that column.
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,601
Members
449,109
Latest member
Sebas8956

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