Do loop to add new item

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
574
Office Version
  1. 365
Platform
  1. Windows
I have a large list of companies (1800+ rows) that I would like to search to see if the company added is in my list. If not, I want to add it. My code is listed below, but it takes quite a while to get through and then crashes on the Activecell.offset line in the do loop. What can I do to make this work.

Code:
    Sheets("Data").Range("C1").Select


        Do
            If ActiveCell.Value = Sheets("Data").Range("AA1").Value Then
                Exit Do
            ElseIf ActiveCell.Value <> Sheets("Data").Range("AA1").Value Then
                ActiveCell.Offset(1, 0).Select
            End If
        Loop

The code after this do loop will add the new company to the list. Please tell me why it crashes in the middle on the "ActiveCell.Offset(1, 0).Select" line. I get a Run-time error '1004' Application-defined or object-defined error.

Thanks for the help,

Robert
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
So are you wanting to search Sheet("Data) column C for the value in:
Sheets("Data").Range("AA1")

And if not found add Range("AA1") value to the bottom of the column C range?

Is that what you want?
 
Upvote 0
Try this:
Code:
Sub My_Find()
'Modified  8/20/2018  6:04:02 PM  EDT
Application.ScreenUpdating = False
Dim SearchString As String
Dim SearchRange As Range
SearchString = Sheets("Data").Range("AA1").Value
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
Set SearchRange = Range("C1:C" & Lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then Cells(Lastrow + 1, "C").Value = SearchString
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Yes. If the value in cell AA1 is found in column C then exit the sub. If it is not found, then add it to the bottom.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top