Consulting

Results 1 to 6 of 6

Thread: VBA to Find and Replace Only in 1 Column

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location

    VBA to Find and Replace Only in 1 Column

    I need a simple Find and Replace VBA code for excel. Dealing with a lot of text in Column H but need to search and replace a few words

    [vba]
    Sub FindReplace()

    Dim a As Long

    For Each a In ActiveSheet.UsedRange

    a = Replace(a, "Find", "Replace")

    Next

    End Sub
    [/vba]

    Found that code on google simple and works by searching all cells in all columns

    but i need it to only search column H

    so i tryed

    [vba]
    Sub FindReplace()

    Dim a As Long

    For Each a In ActiveSheet.Range("H:H")

    a = Replace(a, "Find", "Replace")

    Next

    End Sub
    [/vba]

    but seems to make now an endless loop ?

    PLZ any help ?? sorry not very clued up on vba and been trying to sort this for a few days ..... just recording a macro of search and replace all falls over due to the amount of text in the cell (get a Formula to long Error)

    the top code works but causes problems by searching all cells in all columns

    Using Excel 2003

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Activesheet.Columns("H").Replace _
    What:="find", Replacement:="replace"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location
    Hi thanks for quick reply thats the same code as what recording a macro of Find and Replace which does like the first 3 then falls over as if you do it manualy you gives you a Formula To Long Error due to the amount of text in the cell

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And does this not fall over?

    [vba]

    Sub FindReplace()

    Dim a As Long

    For Each a In ActiveSheet.Columns("H").Cells

    a = Replace(a, "Find", "Replace")
    Next
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location
    ye sorry just seen in my copying rush i made a boo boo Dim a As Long is ment to be Dim a As Range

    i was messing around and forgot to change it back

    ur above code seems to work just that the pc i m on is crappy and seems excel is now not responding :P lol if it aint software its hardware problems

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    See if this is better

    [vba]

    Sub FindReplace()

    Dim a As Range

    With ActiveSheet

    For Each a In .Range(.Range("H1"), .Range("H1").End(xlDown))

    a = Replace(a, "Find", "Replace")
    Next
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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