Hi
I've been searching but without much joy.
What I'm trying to do in vba is search for a number and then return what the row number is for that number.
Hopefully someone can help.
Many thanks
Hi
I've been searching but without much joy.
What I'm trying to do in vba is search for a number and then return what the row number is for that number.
Hopefully someone can help.
Many thanks
Re: Find Value And Return Row Number
stickyfeet,
Though Dave has beaten me to the punch (and I'd say his solution is the way to go), here's my attempt nonetheless:
Sub Macro1()
Dim intMyVal As Integer
Dim lngLastRow As Long
Dim strRowNoList As String
intMyVal = 1 'Value to search for, change as required.
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Search Column A, change as required.
For Each cell In Range("A2:A" & lngLastRow) 'Starting cell is A2, change as required.
If cell.Value = intMyVal Then
If strRowNoList = "" Then
strRowNoList = strRowNoList & cell.Row
Else
strRowNoList = strRowNoList & ", " & cell.Row
End If
End If
Next cell
MsgBox strRowNoList
End Sub
Display More
Regards,
Robert
Re: Find Value And Return Row Number
Gents thanks for such quick replies (this place really is the best).
Spoilt for choice!
Dave and Trebor both worked a treat. Bill didnt get the chance to try yours, but a thanks all the same .... I was struggling trying to find just the one way to get this to work (and spend a long time searching and trying different things!)
Brilliant.
Thanks again
Re: Find Value And Return Row Number
Quote from Trebor76;418783Display Morestickyfeet,
Though Dave has beaten me to the punch (and I'd say his solution is the way to go), here's my attempt nonetheless:
CodeDisplay MoreSub Macro1() Dim intMyVal As Integer Dim lngLastRow As Long Dim strRowNoList As String intMyVal = 1 'Value to search for, change as required. lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Search Column A, change as required. For Each cell In Range("A2:A" & lngLastRow) 'Starting cell is A2, change as required. If cell.Value = intMyVal Then If strRowNoList = "" Then strRowNoList = strRowNoList & cell.Row Else strRowNoList = strRowNoList & ", " & cell.Row End If End If Next cell MsgBox strRowNoList End Sub
Regards,
Robert
Hi Robert,
This is a great code, i am very new to vba, i need help about the output.
Right now the out put is a string with comma, how can i put print them as array or into cells, like A1,B1,C1,D1..., or A1, A2, A3, A4... many thanks in advance.
Re: Find Value And Return Row Number
Welcome to Ozgrid, LynChen.
The policy on this board is you do not post questions in threads started by other members.
Please start your own thread, give it an accurate and concise title that summarises your issue and explain your issue fully. If you think this, or any other thread, can help clarify your issue you can include a link to it by copying the URL from the address bar of your browser and pasting into your message.
Thank you.
Don’t have an account yet? Register yourself now and be a part of our community!