Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2007, 01:11 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Remove numbers from cell

I have a list of 100 (A1:A100) names followed by numbers in the same cell. I would like to create a macro that will remove the numbers but will leave the name alone. For example:

John123 will be changed to John.
Pete12 will be changed to Pete.


Thanks for any help with this.
 
Old January 13th, 2007, 02:07 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Eric

How familiar are you with VBA?

Mike
EchoVue.com
 
Old January 13th, 2007, 02:17 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

If so, the quick and dirty way would be a sub like the following...

Code:
Public Sub RemoveNumbers()
    Dim cellText As String
    Dim i As Integer

    For i = 1 To 100

        Range("A" & i).Select

        'Get the text from the cell
        cellText = Selection.Text

        'As long as there is a number on the right, chop it off
        While IsNumeric(Right(cellText, 1))
            cellText = Left(cellText, Len(cellText) - 1)
        Wend
        'Write the new text string back into the cell
        Selection.Value = cellText
    Next

End Sub
Hope that helps, I'm calling it a night...

Mike

Mike
EchoVue.com
 
Old January 13th, 2007, 12:31 PM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot, that code worked perfectly.

 
Old January 13th, 2007, 12:33 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Glad to help! Welcome to the forum.

Mike
EchoVue.com
 
Old May 9th, 2008, 12:48 PM
Registered User
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there

I need a similar code to this, only I need to delete numbers from the beginning of the cell, together with an underscore.

i.e. I want cell contents text: 219_ugo_italian_SW_hospital.wav
to change to: ugo_italian_SW_hospital.wav

Can you advise on what change is needed to the code above to make it work for me please?

Thanks
Claire
 
Old May 9th, 2008, 07:39 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi clara

Please try the following:

Code:
Public Sub RemoveNumbers_FromLEft()
    Dim cellText As String
    Dim i As Integer

    For i = 1 To 100

        Range("A" & i).Select

        'Get the text from the cell
        cellText = Selection.Text

        'As long as there is a number on the left, chop it off
        While IsNumeric(Left(cellText, 1))
            cellText = Right(cellText, Len(cellText) - 1)
        Wend

        ' chop off the leftmost underscore
        If InStr(1, cellText, "_") Then
              cellText = Right(cellText, Len(cellText) - 1)
        End If

        'Write the new text string back into the cell
        Selection.Value = cellText
    Next

End Sub
Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 10th, 2008, 12:26 PM
Authorized User
 
Join Date: Nov 2007
Posts: 48
Thanks: 0
Thanked 4 Times in 4 Posts
Default

claralou,

Here you go.

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).

Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into the Module1.

Code:
Option Explicit
Sub RemoveNumbersUnderscore()
    Dim lngLastRow As Long
    Application.ScreenUpdating = False
    Range("A1").EntireColumn.Insert
    lngLastRow = Range("B" & Rows.Count).End(xlUp).Row
    With Range("A1:A" & lngLastRow)
        .FormulaR1C1 = "=RIGHT(RC[1],LEN(RC[1])-FIND(""_"",RC[1],1))"
        .Copy
        .Value = .Value
        .Copy Range("B1")
    End With
    Columns("A:A").Delete Shift:=xlToLeft
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

Then run the "RemoveNumbersUnderscore" macro.


Have a great day,
Stan

stanleydgromjr

Windows Vista Business and Excel 2003, 2007.
 
Old June 3rd, 2010, 10:23 AM
Registered User
 
Join Date: Jun 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi All,

I'm trying to carry out a similar task using Excel 2007. I have a column of numbers in the following format.

99.740% (99.875%)

I need to delete the first figure and the brackets so I end up with the bracketed figure but without the brackets. i.e. 99.875%

Can anyone help? Thanks in advance for you time!.

Subs
 
Old June 3rd, 2010, 09:54 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You can use the combination of Mid, Instr and Left Functions

Code:
Sub Extract_Numbers_In_Bracs()

Dim oCell As Range
Dim Sval

For Each oCell In Range("A1:A5")
    If InStr(oCell.Value, "(") And InStr(oCell.Value, "(") Then
        Sval = Mid(oCell.Value, InStr(oCell.Value, "(") + 1, Len(oCell.Value))
        oCell.Value = Left(Sval, InStr(Sval, ")") - 1)
    End If
Next oCell

End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove numbers in XML files using xsl srkumar XSLT 1 April 15th, 2008 06:43 AM
Compare numbers and letters in same cell EricB123 Excel VBA 1 January 21st, 2007 03:30 PM
Remove letters from numbers Corey Access 7 December 18th, 2005 09:09 PM
if the cell content is a part of another cell cont sriramus Excel VBA 1 November 15th, 2005 10:20 AM
Lose cell Text when editing cell in VSFlexGrid 6 bobcratchet VB How-To 0 July 30th, 2004 09:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.