+ Reply to Thread
Results 1 to 9 of 9

Find and Replace when needed Code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-13-2017
    Location
    Greece
    MS-Off Ver
    2007
    Posts
    52

    Find and Replace when needed Code

    Hello guys, at my project (I send a sample file to see it) I have 2 sheets (sheet 1 and sheet 2) . At the sheet 1 there are 2 headings "Rank" and "Name" and below those headings in cells B4 and C4 you can write the Ranking and the Name of a person (for example Rank 1 , Name George). Then there is a button that is titled "Add". When you press it, the data goes to Sheet2 in two columns (A:B) where column A is the Rank and Column B is the Name. The code I used for the button is :

    Sub Button1_Click()
    Dim Rank As String, Name As String
    Worksheets("Sheet1").Select
    Rank = Range("B4")
    Name = Range("C4")
    Worksheets("Sheet2").Select
    Worksheets("Sheet2").Range("A1").Select
    If Worksheets("Sheet2").Range("A1").Offset(1, 0) <> "" Then
    Worksheets("Sheet2").Range("A1").End(xlDown).Select
    End If
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = Rank
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = Name
    Worksheets("Sheet1").Select
    Range("B4").ClearContents
    Range("C4").ClearContents
    End Sub
    Now what I need is that in the same code (when you press the "Add" button) to have a code that will search the range A1:B50 and if it finds a previous entry (for example Rank 1, Name George) it will delete the content of those two cells and will place in that cells (that have been cleared) the new entries for example

    Old data

    1 George
    1 Peter
    1 Michael
    2 John


    New data

    3 Peter

    Should be Now

    1 George
    3 Peter
    1 Michael
    2 John

    Thanks guys for your time
    Attached Files Attached Files

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Find and Replace when needed Code

    Hi,
    Is there any special reason why you do not attach the file WITH the macro?
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    01-13-2017
    Location
    Greece
    MS-Off Ver
    2007
    Posts
    52

    Re: Find and Replace when needed Code

    hmm the file should have the macro let me check, I am at my laptop now and using different excel maybe it's some setting there. Let me check it

  4. #4
    Registered User
    Join Date
    01-13-2017
    Location
    Greece
    MS-Off Ver
    2007
    Posts
    52

    Re: Find and Replace when needed Code

    Yeah sorry for that, it was a mistake I saved it as a normal book and not with activated macros I upload it again.
    Attached Files Attached Files

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find and Replace when needed Code

    This is how I would write that:
    Option Explicit
    
    Sub Button1_Click()
    Dim Rank As String, Name As String, NmFIND As Range
    
    Rank = ThisWorkbook.Sheets("Sheet1").Range("B4").Value
    Name = ThisWorkbook.Sheets("Sheet1").Range("C4").Value
    
    If Rank = "" Or Name = "" Then Exit Sub
    
    With ThisWorkbook.Sheets("Sheet2")
        On Error Resume Next
        Set NmFIND = .Range("B:B").Find(Name, LookIn:=xlValues, LookAt:=xlWhole)
        If Not NmFIND Is Nothing Then
            NmFIND.Offset(, -1).Value = Rank
        Else
            With .Range("A" & .Rows.Count).End(xlUp).Offset(1)
                .Value = Rank
                .Offset(, 1).Value = Name
            End With
        End If
    End With
    
    ThisWorkbook.Sheets("Sheet1").Range("B4:C4").ClearContents
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Find and Replace when needed Code

    See attached file returned, I see that my idea is the same
    Attached Files Attached Files

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Find and Replace when needed Code

    xlsx file is non-macro embedded file, but no problem. You got your answers

  8. #8
    Registered User
    Join Date
    01-13-2017
    Location
    Greece
    MS-Off Ver
    2007
    Posts
    52

    Re: Find and Replace when needed Code

    Nice it works great!! thanks both of you rep added

  9. #9
    Registered User
    Join Date
    01-13-2017
    Location
    Greece
    MS-Off Ver
    2007
    Posts
    52

    Re: Find and Replace when needed Code

    hm I did copy the code to my project but for some reason I get the error 1004 when I push the button :/ can you please check it? at the test file it worked perfectly .

    The tabs we are interrested in are "talents" and "Charactersheet"


    Thanks guys.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find & Replace Loop Help Needed
    By bryanmarks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2014, 10:19 AM
  2. [SOLVED] Formula to find and replace where needed
    By maymano in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-07-2013, 02:09 PM
  3. Help Needed with Find and Replace Macro
    By e30325i in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2013, 02:49 PM
  4. VBA Find and replace loop help needed
    By susanbarbour in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2012, 08:43 AM
  5. [SOLVED] VBA needed to find and replace text using a wildcard
    By jgupte in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-30-2012, 08:10 PM
  6. FIND and REPLACE characters needed
    By Peter C in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 02-10-2006, 03:10 PM
  7. [SOLVED] FIND and REPLACE characters needed
    By Peter C in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-08-2006, 05:20 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1