+ Reply to Thread
Results 1 to 13 of 13

Search for value in a range and overwrite if found and create new if not found

Hybrid View

  1. #1
    Registered User
    Join Date
    12-02-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    19

    Search for value in a range and overwrite if found and create new if not found

    I need help with a macro to do the following:

    Cell B2 = 12/31/2015
    Look in Range A1:A100 for this value (B2)
    If found, overwrite (or do nothing)
    If not found, paste value in last empty cell in range

    Thanks!

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    what does this mean.
    If found, overwrite (or do nothing)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    12-02-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Search for value in a range and overwrite if found and create new if not found

    if the value already exists I would like the macro to do nothing. I just meant if it is easier have the macro just overwrite the existing value (i.e. if 12/31/2015 already exists in the range, paste 12/31/2015 where it already exists). The former is probably easier.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    try this:

    Option Explicit
    
    Sub datefind()
        Dim lr As Long
        Dim i As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Application.ScreenUpdating = False
        For i = 1 To lr
            If Range("A" & i) = Range("B2") Then
                Exit Sub
            Else: Range("A" & lr + 1) = Range("B2")
            End If
        Next i
        Application.ScreenUpdating = True
        MsgBox "Complete"
    End Sub

  5. #5
    Registered User
    Join Date
    12-02-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Search for value in a range and overwrite if found and create new if not found

    works great, thanks!

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks for the rep

  7. #7
    Registered User
    Join Date
    12-02-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Search for value in a range and overwrite if found and create new if not found

    Actually, testing it further it does not seem to work. It pastes the value at the end of the range even if it already exists in the range.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    Suggest you post (upload) a sample worksheet for testing. I ran on a sample I made and it worked as requested.

  9. #9
    Registered User
    Join Date
    12-02-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Search for value in a range and overwrite if found and create new if not found

    See the example sheet attached. It does not add 12/31/2015 to column A regardless of whether it is already in the range or not...
    Attached Files Attached Files

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    The reason it does not work, is you have placed the value to compare in cell B1. Your post #1 indicated that this value is in B2. If you place the date in B2, it will work. If you want to compare it to B1, change the code to read B1 instead of B2.

  11. #11
    Registered User
    Join Date
    12-02-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Search for value in a range and overwrite if found and create new if not found

    Ah, sorry small typo. Even with the value in B2 it does add it to the end of the range but if you run the macro again after it has been added, it continues to add it even though it already exists.
    Attached Files Attached Files

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    Yup! You are right. Will work on this today.

  13. #13
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Search for value in a range and overwrite if found and create new if not found

    Here is a new solution:

    Option Explicit
    
    Sub datefind()
        Dim lr As Long
        Dim i As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Application.ScreenUpdating = False
        Dim res As Variant
        On Error Resume Next
        res = Application.WorksheetFunction.Index(Range("A1" & ":A" & lr), Application.WorksheetFunction.Match(Range("B2"), Range("A1" & ":A" & lr), 0))
        If Err = 0 Then
            Exit Sub
        Else: Range("A" & lr + 1) = Range("B2")
        End If
        Application.ScreenUpdating = True
        MsgBox "Complete"
    End Sub

+ 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. Replies: 10
    Last Post: 11-04-2015, 05:19 AM
  2. Find value and overwrite existing value if found else create new record Column 2 row
    By iamerror in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-09-2015, 05:14 AM
  3. Replies: 2
    Last Post: 09-13-2013, 11:02 AM
  4. [SOLVED] vba overwrite text in string if found in a range using adjacent values ( inside same cell)
    By vlady in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-09-2013, 11:32 PM
  5. [SOLVED] Search for value in range and clear value when found
    By 3foo3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-30-2013, 08:27 PM
  6. Macro to search Workbook and create link to found data
    By gosharp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-07-2012, 06:29 AM
  7. Create a search box that jumps to items found in list
    By cadmancan in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-27-2009, 11:44 AM

Tags for this Thread

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