Solved

Excel VBA Help - Removing specific keyword

Posted on 2012-04-04
7
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

734 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