Search for a string, find the column and use that column to sort by

  • Thread starter Thread starter sidhargrove
  • Start date Start date
S

sidhargrove

I have a data dump into excel, but the info doesn't dump into the same
column everytime, so I need to search the sheet for that string and
once I find the column, use that column to sort the data.
 
One way you may try is to record a macro while doing a manual
edit>FIND>sort. Then modify.
 
Sub findSort()
Dim findWhat As String, col As Integer
findWhat = "Johnny"
On Error GoTo errHandler
col = Cells.Find(What:=findWhat, After:=Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
Cells.Sort Key1:=Columns(col), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Exit Sub
errHandler:
MsgBox findWhat & " not found"
End Sub
 
Thanks a lot. I should have said filter instead of sort, but I think
I got it from here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top