PC Review


Reply
Thread Tools Rate Thread

Convert data to array

 
 
GeraldM
Guest
Posts: n/a
 
      2nd May 2008
Would appreciate help with the following problem:

I have data in two columns - "Make" & "Model"
I need to convert the into an array with:
1) a column for each "Make"
2) each model in the correct column.

Example:

Change the data from that shown below: (Note: The number of "Makes" is not
limited to three).

Make Model
------- ----------
Ford Falcon
Ford Focus
Ford Fiesta
Ford Mondeo
Ford Anglia
Holden Commodore
Holden Torrano
Holden Monarro
Holden Astra
Kia Rio
Kia Carnival


Need to change the data to look like below: (Note: I need to automate this
(using VBA) for others to use, so I don't want to manually transpose the
data).

Ford Holden Kia
-------- ------------- -----------
Falcon Commodore Rio
Focus Torrano Carnival
Fiesta Monarro
Mondeo Astra
Anglia

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      2nd May 2008
The code does almost everything but creates the original data. Just set the
first three lines of code to the correct Rows and columns and the code does
the rest. the code assumes the original data is in sheet 1 and the new data
is created iin sheet 2. You can change the names of the sheets as required.
the code has been fully tested.

the code searches row 1 of sheet 2 to find the Make of the automobiles. If
it finds the Make it adds the model to the end of the list. If it doesn't
find the Make it adds it to the header Row on Sheet 2 and addss the Model to
the first Row.


Sub make_table()

Sh1StartRow = 1 'first row of data after header
Sh2FirstRow = 2 'leave at least one row for headers
Sh2NewCol = 1 'enter column where you want data to start

Sh1RowCount = Sh1StartRow
With Sheets("Sheet1")
Do While .Range("A" & Sh1RowCount) <> ""
Make = .Range("A" & Sh1RowCount)
Model = .Range("B" & Sh1RowCount)
With Sheets("Sheet2")
Set c = .Rows(1).Find(what:=Make, _
LookIn:=xlValues, lookat:=xlWhole)
If c Is Nothing Then
.Cells(1, Sh2NewCol) = Make
.Cells(Sh2FirstRow, Sh2NewCol) = Model
Sh2NewCol = Sh2NewCol + 1
Else
LastRow = .Cells(Rows.Count, c.Column).End(xlUp).Row
.Cells(LastRow + 1, c.Column) = Model
End If
End With
Sh1RowCount = Sh1RowCount + 1
Loop
End With
End Sub

"GeraldM" wrote:

> Would appreciate help with the following problem:
>
> I have data in two columns - "Make" & "Model"
> I need to convert the into an array with:
> 1) a column for each "Make"
> 2) each model in the correct column.
>
> Example:
>
> Change the data from that shown below: (Note: The number of "Makes" is not
> limited to three).
>
> Make Model
> ------- ----------
> Ford Falcon
> Ford Focus
> Ford Fiesta
> Ford Mondeo
> Ford Anglia
> Holden Commodore
> Holden Torrano
> Holden Monarro
> Holden Astra
> Kia Rio
> Kia Carnival
>
>
> Need to change the data to look like below: (Note: I need to automate this
> (using VBA) for others to use, so I don't want to manually transpose the
> data).
>
> Ford Holden Kia
> -------- ------------- -----------
> Falcon Commodore Rio
> Focus Torrano Carnival
> Fiesta Monarro
> Mondeo Astra
> Anglia
>

 
Reply With Quote
 
GeraldM
Guest
Posts: n/a
 
      3rd May 2008
Thanks Joel.
Works perfectly.

Regards: Gerald

"Joel" wrote:

> The code does almost everything but creates the original data. Just set the
> first three lines of code to the correct Rows and columns and the code does
> the rest. the code assumes the original data is in sheet 1 and the new data
> is created iin sheet 2. You can change the names of the sheets as required.
> the code has been fully tested.
>
> the code searches row 1 of sheet 2 to find the Make of the automobiles. If
> it finds the Make it adds the model to the end of the list. If it doesn't
> find the Make it adds it to the header Row on Sheet 2 and addss the Model to
> the first Row.
>
>
> Sub make_table()
>
> Sh1StartRow = 1 'first row of data after header
> Sh2FirstRow = 2 'leave at least one row for headers
> Sh2NewCol = 1 'enter column where you want data to start
>
> Sh1RowCount = Sh1StartRow
> With Sheets("Sheet1")
> Do While .Range("A" & Sh1RowCount) <> ""
> Make = .Range("A" & Sh1RowCount)
> Model = .Range("B" & Sh1RowCount)
> With Sheets("Sheet2")
> Set c = .Rows(1).Find(what:=Make, _
> LookIn:=xlValues, lookat:=xlWhole)
> If c Is Nothing Then
> .Cells(1, Sh2NewCol) = Make
> .Cells(Sh2FirstRow, Sh2NewCol) = Model
> Sh2NewCol = Sh2NewCol + 1
> Else
> LastRow = .Cells(Rows.Count, c.Column).End(xlUp).Row
> .Cells(LastRow + 1, c.Column) = Model
> End If
> End With
> Sh1RowCount = Sh1RowCount + 1
> Loop
> End With
> End Sub
>
> "GeraldM" wrote:
>
> > Would appreciate help with the following problem:
> >
> > I have data in two columns - "Make" & "Model"
> > I need to convert the into an array with:
> > 1) a column for each "Make"
> > 2) each model in the correct column.
> >
> > Example:
> >
> > Change the data from that shown below: (Note: The number of "Makes" is not
> > limited to three).
> >
> > Make Model
> > ------- ----------
> > Ford Falcon
> > Ford Focus
> > Ford Fiesta
> > Ford Mondeo
> > Ford Anglia
> > Holden Commodore
> > Holden Torrano
> > Holden Monarro
> > Holden Astra
> > Kia Rio
> > Kia Carnival
> >
> >
> > Need to change the data to look like below: (Note: I need to automate this
> > (using VBA) for others to use, so I don't want to manually transpose the
> > data).
> >
> > Ford Holden Kia
> > -------- ------------- -----------
> > Falcon Commodore Rio
> > Focus Torrano Carnival
> > Fiesta Monarro
> > Mondeo Astra
> > Anglia
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert excel data to 2-D array using macro =?Utf-8?B?RGF2aWQ=?= Microsoft Excel Programming 4 13th Nov 2005 02:01 PM
How to convert a structure including an array into a byte stream / array? ORC Microsoft Dot NET Compact Framework 2 2nd Nov 2004 07:43 PM
How to Convert DataRow with Byte Array data to String? Paul W Microsoft ASP .NET 1 6th Aug 2004 07:55 PM
Convert array of objects into data grid =?Utf-8?B?Qm9i?= Microsoft ADO .NET 3 16th Jan 2004 05:05 PM
can I convert an object/struct to raw data (byte array) and back again? Chris LaJoie Microsoft C# .NET 1 3rd Sep 2003 07:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:43 AM.