Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA Help - Removing specific keyword

Posted on 2012-04-04
7
Medium Priority
?
216 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, CCNA
  • 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, CCNA
ID: 37807363
I also need it to replace the string immediately to the right of the word "Printer".  How would I accomplish that?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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, CCNA
ID: 37807423
Just the word immediately to the right of the word printer.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 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, CCNA
ID: 37807826
Works perfect.  I never would've thought of that!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

772 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