Results 1 to 3 of 3

Thread: Excel Find/Replace issue ....

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Question Excel Find/Replace issue ....

    I've got the following piece of code that picks up a graphical "up arrow", and replaces any cells with the value of "U" with this graphic.

    VB Code:
    1. intY = 1
    2.         intX = 0
    3.         .Range("K8").Select
    4.         .ActiveSheet.Shapes("UpArrow").Select
    5.         .Selection.Copy
    6.        
    7.         On Error GoTo Next_Type
    8.         With .ActiveSheet
    9.                 .Cells.Find(What:="U", After:=objExcel.ActiveCell, LookIn:=xlFormulas, LookAt _
    10.                     :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    11.                     False).Activate
    12.             Do Until intX >= intY
    13.                 intX = objExcel.ActiveCell.row - 1
    14.                 tmpRow = intX
    15.                 tmpCol = objExcel.ActiveCell.Column
    16.                 objExcel.Selection.PasteSpecial xlPasteAll
    17.                 .Cells.FindNext(After:=objExcel.ActiveCell).Activate
    18.                 If tmpRow = objExcel.ActiveCell.row - 1 And tmpCol = objExcel.ActiveCell.Column Then Exit Do
    19.                 intY = objExcel.ActiveCell.row
    20.             Loop
    21.         End With

    The problem I've got is this .... if the only "U" cells are on the same row then the routine goes round and round in circles.
    It is unlikely, unfortunately it HAS just happened (typical Monday !) !!!

    I don't actually like this way of doing things anyway, as it searches row by row and takes a few minutes to complete.

    Does anyone know of a better way of replacing "U" cells with my "UpArrow" graphic ?

    A single Find/Replace after highlighting the affected rows would be ideal, but I don't know how to assign my "UpArrow" graphic in the "Replace" box (I'm assuming you can't do it this way).

    Can anyone help ?

    Thanks in advance ....

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    This is the basis of the usual Find/Replace method (copied from VB Editor Help) :-
    Code:
    With Worksheets(1).Range("a1:a500")
        Set c = .Find(2, lookin:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Interior.Pattern = xlPatternGray50
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
    Regards
    BrianB
    -------------------------------

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Hi,

    I had this type of problem.. Its because the find doesn't move the cell...

    Which is weird because it looks like you are checking for the cell being on the same row...
    VB Code:
    1. Do Until intX >= intY
    2.                 intX = objExcel.ActiveCell.row - 1
    3.                 tmpRow = intX
    4.                 tmpCol = objExcel.ActiveCell.Column
    5.                 objExcel.Selection.PasteSpecial xlPasteAll
    6.                 .Cells.FindNext(After:=objExcel.ActiveCell).Activate
    7.                 If tmpRow = objExcel.ActiveCell.row - 1 And tmpCol = objExcel.ActiveCell.Column Then Exit Do
    8.                 intY = objExcel.ActiveCell.row
    9.             Loop
    the do loop is infinite - mainly because if you are on the same cell, then inty is always one more than intx.

    So the problem lies with the if statement and exiting.

    If you put a breakpoint on it and see what the values are it will probably help you.
    Alternatively, grab the address from the active cell and see if it matches the active cell after the find. If they are the same, leave the loop (or put it into the do until part).


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width