Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA Help - Removing specific keyword

Posted on 2012-04-04
7
Medium Priority
?
210 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

705 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