Excel VBA to Find Data and Insert specitic data in cell to the right

Posted on 2011-05-03
Last Modified: 2012-06-21
I have a piece of Excel VBA code that does a global search and replace for a variety of values.  

Once this is done, I want to also have the macro insert a specific value in the column to the left of the found values... For instance, I have 100 rows with a value of "468x60", I want the macro to insert to the column on the left the value "CPM"...  Then when it finds the next 100 rows of "Messenger" I want the macro to replace the value to the left with "Messenger Ad"....

Any help would be appreciated!!!
Sub FixDimension()
    Cells.Replace What:="Full Banner - 468 x 60", Replacement:="468x60", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Skyscraper - 120 x 600", Replacement:="120x600", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Medium Rectangle - 300 x 250", Replacement:="300x250", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Super Banner - 728 x 90", Replacement:="728x90", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="GEOPOP - 1 x 1", Replacement:="GeoPop", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="780x260 - 780 x 260", Replacement:="Bottom of Page", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Slider - 1 x 1", Replacement:="Messenger Ad", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Raw Click - 1 x 1", Replacement:="Raw Click", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Interstitial - 1 x 1", Replacement:="Interstitial", LookAt:=xlWhole, MatchCase:=False
    Cells.Replace What:="Pixel/Popup - 1 x 1", Replacement:="Pop Under", LookAt:=xlWhole, MatchCase:=False
End Sub

Open in new window

Question by:adworldmedia
    1 Comment
    LVL 24

    Accepted Solution

    This perhaps? You don't say what you want inserted to the left, but the values would go in sOffset.
    Sub FixDimension()
    Dim rFind As Range, sFind(), sReplace(), sOffset(), i As Long
    sFind = Array("Full Banner - 468 x 60", "Skyscraper - 120 x 600", "Medium Rectangle - 300 x 250", _
                  "Super Banner - 728 x 90", "GEOPOP - 1 x 1", "780x260 - 780 x 260", _
                  "Slider - 1 x 1", "Raw Click - 1 x 1", "Interstitial - 1 x 1", "Pixel/Popup - 1 x 1")
    sReplace = Array("468x60", "120x600", "300x250", "728x90", "GeoPop", "Bottom of Page", _
                     "Messenger Ad", "Raw Click", "Interstitial", "Pop Under")
    sOffset = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
    With ActiveSheet.UsedRange
        For i = LBound(sFind) To UBound(sFind)
            Set rFind = .Find(What:=sFind(i), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
            If Not rFind Is Nothing Then
                    rFind.Value = sReplace(i)
                    rFind.Offset(, -1) = sOffset(i)
                    Set rFind = .FindNext(rFind)
                Loop While Not rFind Is Nothing
            End If
        Next i
    End With
    End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Sometimes we don't want to show zeros in our Excel spreadsheets. This is sometimes most evident in our charts. Look at the chart below, all the zero values are visible. I think that all will agree with the fact that zero values are not looking nice …
    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…
    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.

    779 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

    16 Experts available now in Live!

    Get 1:1 Help Now