Sort by header

J

Jackanorry

I have a short data range - 6 numbers - that I want to import into a spread
sheet and have them automatically sort to there respective header.
i.e.
3 5 7 12 21 33 - (no number would ever be repeated)

numbers in top row (header) from 1 to 100


I've attempted working with Macro's, thinking that would be the easiest way
to set this up, but Macros are beyond me.

TIA
John
 
B

broro183

hi Jack,

Try inserting the below code in a normal module in the file that yo
want to be modified*., go to the file containing the six cells, selec
the cells, press [alt + F8] to bring up a macro dialog box & selec
ImportCellsBasedOnHdrRow & press [Run].

Option Explicit
Sub ImportCellsBasedOnHdrRow()
Dim rng As Range
Dim cll As Range
Dim MasterSht As Worksheet
Dim RowToUse As Long
Dim ColToUse As Long
'define the variables
Set MasterSht = ThisWorkbook.Worksheets("sheet1") 'change this to b
the file & sheet that the information is to be added to...
Set rng = Selection
'check that data is selected
If TypeName(rng) <> "Range" Then GoTo Exitsub
'loop through each cell within the selection (possibly in
separate file)
For Each cll In rng
With MasterSht
RowToUse = LastCell(MasterSht).Row
ColToUse = IdHdrColumn(.Range("1:1"), cll.Value2)
.Cells(RowToUse, ColToUse).Value2 = cll.Value2
End With
Next cll
Exitsub:
Set rng = Nothing
Set MasterSht = Nothing
End Sub

Private Function IdHdrColumn(HdrRow As Range, TextToFind As String) A
Long
On Error GoTo ErrHandler
With HdrRow
IdHdrColumn = .Find(What:=TextToFind, lookat:=xlWhole
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
'check that the respective column has not already bee
populated
If .Resize(1, 1).Offset(1, IdHdrColumn - 1).Value <> "" The
GoTo ErrHandler
End With
Exit Function
ErrHandler:
'assign the next blank column if the value is not found as a heade
string
With HdrRow.Parent
IdHdrColumn = .Cells(HdrRow.Row
.Columns.Count).End(xlToLeft).Offset(0, 1).Column
End With
On Error GoTo 0
End Function

private Function LastCell(ws As Worksheet) As Range
' sourced from 'Beyond Technology :: Microsoft Excel - Identifying th
Real Last Cell' (http://www.beyondtechnology.com/geeks012.shtml)
'to identify the lastcell on a worksheet (& not necessarily the activ
sheet)
Dim LastRow As Long
Dim LastCol As Long
' Error-handling is here in case there is not any
' data in the worksheet
On Error Resume Next
With ws
' Find the last real row
LastRow = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
LastRow = Application.WorksheetFunction.Max(1, LastRow)
' Find the last real column
LastCol = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
LastCol = Application.WorksheetFunction.Max(1, LastCol)
' Finally, initialize a Range object variable for
' the last populated row.
Set LastCell = .Cells(LastRow, LastCol)
End With
On Error GoTo 0
End Function

*Have a read of the below link for some initial understanding o
macros:
'Getting Started with Macros and User Defined Functions
(http://www.mvps.org/dmcritchie/excel/getstarted.htm)

hth
Ro
 
J

Jackanorry

Rob,

I appreciate the help. I must be missing something as I'm getting errors -
syntax errors specifically. I will post in the morning when I'm a little more
cognizant.

Thanks again for the swift response,

John



broro183 said:
hi Jack,

Try inserting the below code in a normal module in the file that you
want to be modified*., go to the file containing the six cells, select
the cells, press [alt + F8] to bring up a macro dialog box & select
ImportCellsBasedOnHdrRow & press [Run].

Option Explicit
Sub ImportCellsBasedOnHdrRow()
Dim rng As Range
Dim cll As Range
Dim MasterSht As Worksheet
Dim RowToUse As Long
Dim ColToUse As Long
'define the variables
Set MasterSht = ThisWorkbook.Worksheets("sheet1") 'change this to be
the file & sheet that the information is to be added to...
Set rng = Selection
'check that data is selected
If TypeName(rng) <> "Range" Then GoTo Exitsub
'loop through each cell within the selection (possibly in a
separate file)
For Each cll In rng
With MasterSht
RowToUse = LastCell(MasterSht).Row
ColToUse = IdHdrColumn(.Range("1:1"), cll.Value2)
.Cells(RowToUse, ColToUse).Value2 = cll.Value2
End With
Next cll
Exitsub:
Set rng = Nothing
Set MasterSht = Nothing
End Sub

Private Function IdHdrColumn(HdrRow As Range, TextToFind As String) As
Long
On Error GoTo ErrHandler
With HdrRow
IdHdrColumn = .Find(What:=TextToFind, lookat:=xlWhole,
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
'check that the respective column has not already been
populated
If .Resize(1, 1).Offset(1, IdHdrColumn - 1).Value <> "" Then
GoTo ErrHandler
End With
Exit Function
ErrHandler:
'assign the next blank column if the value is not found as a header
string
With HdrRow.Parent
IdHdrColumn = .Cells(HdrRow.Row,
.Columns.Count).End(xlToLeft).Offset(0, 1).Column
End With
On Error GoTo 0
End Function

private Function LastCell(ws As Worksheet) As Range
' sourced from 'Beyond Technology :: Microsoft Excel - Identifying the
Real Last Cell' (http://www.beyondtechnology.com/geeks012.shtml)
'to identify the lastcell on a worksheet (& not necessarily the active
sheet)
Dim LastRow As Long
Dim LastCol As Long
' Error-handling is here in case there is not any
' data in the worksheet
On Error Resume Next
With ws
' Find the last real row
LastRow = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
LastRow = Application.WorksheetFunction.Max(1, LastRow)
' Find the last real column
LastCol = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
LastCol = Application.WorksheetFunction.Max(1, LastCol)
' Finally, initialize a Range object variable for
' the last populated row.
Set LastCell = .Cells(LastRow, LastCol)
End With
On Error GoTo 0
End Function

*Have a read of the below link for some initial understanding of
macros:
'Getting Started with Macros and User Defined Functions'
(http://www.mvps.org/dmcritchie/excel/getstarted.htm)

hth
Rob


--
broro183

Rob Brockett. Always learning & the best way to learn is to
experience...
------------------------------------------------------------------------
broro183's Profile: http://www.thecodecage.com/forumz/member.php?userid=333
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=149819

.
 
B

broro183

hi John,

It may be that some of the comments are not wrapping when being copy
pasted into your code, or more probably, it could be that I'm using thi
code in Excel 2007 & it may have some extra arguments that aren'
identified in older versions of excel.

Let us know which lines highlight when you step through the code wit
the [F8] key (or try to compile it using [alt + D + L]) in the VBE.

Ro
 
J

Jackanorry

Rob,
some of the comments weren't wrapping - i think i have that sorted.
I'm using 2007

Highlighted : ActiveCell.Offset(-4, -8).Range("A1").Select

Thanks again,

John
 
B

broro183

hi John,

Sorry about the delayed response.
Jackanorry;545992 said:
Highlighted : ActiveCell.Offset(-4, -8).Range("A1").Select
There is nothing in the code I provided that looks anything lik
this... where is it from?
Can you please post all of the code you are using?

At a glance, the error in this line of code would occur if th
activecell is in any cell closer to "a1" than cell H4 (ie cells(4,8)).

hth
Ro
 

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

Top