Solved

Excel VBA Help - Removing specific keyword

Posted on 2012-04-04
7
192 Views
Last Modified: 2012-04-04
Hi All,

I receive a list each day of about 60,000 line items now, and I need to remove the word printer the string immediately to the right (the printer serial number).  Is there any easy way to do this?  The macro I wrote using the TRIM function is wiping out everything to the right, which obviously won't work since the word printer could be anywhere in the string.  An example workbook is attached.

Option Explicit

Sub RemoveKWB()
Dim wksData As Worksheet
Dim rngData As Range
Dim rngCell As Range
'// Change sheetname to suit //
Set wksData = ThisWorkbook.Worksheets("Sheet2")
'// Presumes a header row //
With wksData
Set rngData = .Range("B2:B" & _
Application.Max(2, .Cells(.Rows.Count, 1).End(xlUp).Row))
End With

For Each rngCell In rngData
If InStr(1, rngCell.Value, "Printer") > 0 Then

rngCell.Value = _
Trim(Left(rngCell.Value, InStr(1, rngCell.Value, "Printer") - 1))
End If
Next
End Sub
Asset-Example---Macro-Issue.xlsx
0
Comment
Question by:Brad Sims
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 37806683
try using the replace function

rngCell.value = replace(rngCell.value,"Printer","")

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37806685
Try changing:

    rngCell.Value = Trim(Left(rngCell.Value, InStr(1, rngCell.Value, "Printer") - 1))

to

    rngCell.Value = Replace(rngCell.Value, "Printer ", "")

I hope this helps.
0
 
LVL 7

Author Comment

by:Brad Sims
ID: 37807363
I also need it to replace the string immediately to the right of the word "Printer".  How would I accomplish that?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 37807377
John,

Do you need to remove everything to the right of the word "Printer"? Or only the word to the right of the word "Printer"?
0
 
LVL 7

Author Comment

by:Brad Sims
ID: 37807423
Just the word immediately to the right of the word printer.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37807488
Please try this:
Option Explicit

Sub RemoveKWB()
    Dim wksData As Worksheet
    Dim rngData As Range
    Dim rngCell As Range
    Dim re As Object
    
    Set re = CreateObject("vbscript.regexp")
    re.Pattern = "Printer \w+(, )?"
    
    '// Change sheetname to suit //
    Set wksData = ThisWorkbook.Worksheets("Sheet2")
    '// Presumes a header row //
    With wksData
        Set rngData = .Range("B2:B" & _
        Application.Max(2, .Cells(.Rows.Count, 1).End(xlUp).Row))
    End With
    
    For Each rngCell In rngData
        If InStr(1, rngCell.Value, "Printer") > 0 Then
            rngCell.Value = re.Replace(rngCell.Value, "")
        End If
    Next
End Sub

Open in new window

0
 
LVL 7

Author Closing Comment

by:Brad Sims
ID: 37807826
Works perfect.  I never would've thought of that!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question