Solved

Excel VBA Help - Removing specific keyword

Posted on 2012-04-04
7
162 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:John15-16
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:Anthony Berenguel
Comment Utility
try using the replace function

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

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
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:John15-16
Comment Utility
I also need it to replace the string immediately to the right of the word "Printer".  How would I accomplish that?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:Anthony Berenguel
Comment Utility
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:John15-16
Comment Utility
Just the word immediately to the right of the word printer.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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:John15-16
Comment Utility
Works perfect.  I never would've thought of that!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now