Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

vba searching list

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Vba searching a list

Vba searching a list

ResolvedVersion 2007

Aquila has attended:
Excel VBA Intro Intermediate course

Vba searching a list

hi

I would like my VBA code to search a list for a specified code(number), and if it is not there, should add it onto the bottom of the list. Any suggestions on how I would go about doing this?

RE: vba searching a list

Hi Aquila, thanks for your query. It's a little difficult to write specific code for you, but below is the logic of just one way of possibly achieving what you want:

Dim MyFoundValue As Integer

MyFoundValue = 0
'Select the range of cells containing your list
'Count the number of rows using CurrentRegion.Count
'Set up a loop and move the active cell down the list

For myrow = 1 to Range.CurrentRegion.Rows.Count

If ActiveCell.Value = "myvalue" Then
MyFoundValue = 1
End If
Else

Next row

End If

If MyFoundValue = 0 Then

Offset the row number by 1 (i.e. go to the first empty row under the list)

ActiveCell.Value = "myvalue"

Hope this helps,

Anthony

RE: vba searching a list

Thanks Anthony - that makes perfect sense! Did manage to create my own code which worked - but was a little messy. I like the idea of how we define myFoundValue, and the result of whether we add it on list depends on this value.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Hiding and unhiding columns using the keyboard

CTRL + 0 hides your columns and CTRL + SHIFT + ) unhides them although you would need to highlight the column letters either side as per normal

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.