Results 1 to 12 of 12

Thread: Find and Replace Excel VBA - replace values???[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Resolved Find and Replace Excel VBA - replace values???[RESOLVED]

    Using Office 2007, but writing code for excel 2003 in work.

    I am trying to replace any cell with the #N/A error, with a unique number
    so i will find the first N/A then replace it with 001, then the next would be 002

    the problem i am having is when i attempt to find and replace, it isnt allowing me to replace values...
    is this new to 2007? when i use find, i can select values, just doesnt show in the drop down for replace.

    hope this had made sense, i tend to go off on a tangent lol
    Last edited by Mitch_s_s; May 8th, 2007 at 01:52 PM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Find and Replace Excel VBA - replace values???

    looks like i'm going to have to loop through the range of cells, which is gonna take a while
    was looking to use excels built in function.

    if anyone has any ideas, please let me know.

    i was thinking for-next loop
    if cell.value = "#N/A" then cell.value = unique_ref
    next cell etc?

    what you think?

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Find and Replace Excel VBA - replace values???

    Yes

    you can use replace values but before that you need to convert #N/A to text format.

    Lets say the data is from B1:B7 then try this...

    vb Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. Dim count As Integer
    4.  
    5. 'paste values
    6. Range("B1:B7").Copy
    7. Range("B1:B7").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    8.         :=False, Transpose:=False
    9.  
    10. 'replace "#N/A" with "replword"
    11. Cells.Replace What:="#N/A", Replacement:="replword", LookAt:=xlPart, _
    12. SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    13. ReplaceFormat:=False
    14.  
    15. 'unique number
    16. count = 1
    17.  
    18. For i = 1 To 7
    19. If Trim(Range("b" & i).Value) = "replword" Then
    20.     Range("b" & i).Value = count
    21.     count = count + 1
    22. End If
    23. Next i
    24. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Find and Replace Excel VBA - replace values???

    cheers man, i knew you'd know!!!

    will check it out, and post back!!

    nice one!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Find and Replace Excel VBA - replace values???

    ended up with this
    can i trim this down, or does it have to find it first, i select cell I1 each time, i try to keep away from selectin cells, but this would miss the first instance if i didnt (wouldnt it :s)
    vb Code:
    1. Columns("I:I").Find(What:="#N/A", After:=ActiveCell, LookIn:=xlFormulas, _
    2.         LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    3.         MatchCase:=False, SearchFormat:=False).Replace What:="#N/A", Replacement:=unique_ref, LookAt:=xlPart, _
    4.         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    5.         ReplaceFormat:=False

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Find and Replace Excel VBA - replace values???

    Hi Mitch

    Like I said earlier, you need to convert #N/A to text and then replace it.

    Also, how will the unique ref change automatically with your code?

    That is the reason why you will have to use a loop like i suggested above and increase the value of the unique reference...

    You can also use Application.Screenupdating = False in the begining of the code to speed up things... and at the end of the code set it to 'true' again...

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Find and Replace Excel VBA - replace values???

    gutted, after all the messing round they dont need it any more haha!

    at least i learnt somethin!

    tar skids!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Find and Replace Excel VBA - replace values???[RESOLVED]

    oh and i already had the loop sorted
    it would add 1 each time it replaced

  9. #9
    New Member
    Join Date
    Nov 2011
    Posts
    14

    Re: Find and Replace Excel VBA - replace values???[RESOLVED]

    Hi,

    I have a somewhat similar problem, and I am fairly new to VB scripting.
    Although I do have a coding background.

    Anyways, the problem at hand is:-

    I have a column of floor codes, with entries like:-

    4th floor
    3rd floor
    5th floor
    29th floor

    etc... now, i need to write a script like the stock find and replace provided in excel which replaces every instance of 4th with Fourth and similarly 3rd with Third etc....

    Can anyone please help me with it??

    I believe, it is fairly easy if someone knows scripting, since all that needs to be done is just specify a range and lots of if cases or using the in between function etc.

    Please help me out.

    Many Thanks.
    Andy

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Find and Replace Excel VBA - replace values???[RESOLVED]

    >>>Although I do have a coding background.

    Say What? I will give you the code but let's do one thing. You show me what you tried and then we will take it form there?

    BTW, welcome to the forums Andy

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11
    New Member
    Join Date
    Apr 2012
    Posts
    1

    Re: Find and Replace Excel VBA - replace values???[RESOLVED]

    OK, very different approach here, but if the #N/A is a formula result, why not trap it at source? I tend to wrap any functions which might return an error in a condition (eg IF(ISNA(function),Alternative Result,function). This can also be useful when wanting to categorise a big chunk of data, for example when you have 3 or 4 lists of account numbers with categories, but you know that there will still be account numbers not on any list. A nicely nested ISNA VLOOKUP function allows you to categorise everything and put anything which is totally unfound into 'Other' or 'corporate', type 9999 or whatever.
    I'm a lazy programmer. If I find myself having to clear errors after the fact, I'd rather find a way of stopping them happening in the first place.

  12. #12
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Re: Find and Replace Excel VBA - replace values???[RESOLVED]

    I would love to stop the errors on my worksheets, BUT the errors occur when macro to get the data is run, it is retrieving data from a database. Example of formula in several cells is {=PISampDat($AY$8,AF$1,$AF$2,$AG$1,0,)} If there is no data to retrieve, other cells that have formula's to calculate the data retrieved from these cells end up with #N/A, then macro which places this value somewhere else places #N/A in those cells. I'm at a loss, thought this forum could help but it didn't. Wish I knew how to post a question???

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