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!
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!
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
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.
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
works great, thanks!
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
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.
Suggest you post (upload) a sample worksheet for testing. I ran on a sample I made and it worked as requested.
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...
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.
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.
Yup! You are right. Will work on this today.
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks