vba searching list

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

Vba searching a list

resolvedResolved · High Priority · Version 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 a worksheet in Excel

Want to circulate an Excel workbook file but keep certain worksheets hidden from your colleagues' view?

You can do so by bringing up the sheet you wish to hide on your screen; then going to Format - Sheet - Hide.

It will not be immediately obvious that a sheet is hidden from view unless perhaps the sheet are still labelled Sheet 1, Sheet 2 etc.

To display the sheet again, you can go to Format - Sheet - Unhide on any of the other sheets in the workbook. A dialogue box will appear, allowing you to select the hidden sheet/s. Click OK to make the sheet/s reappear again.

View all Excel hints and tips


Server loaded in 0.07 secs.