Help Designing a Macro to Align Data

A1MBand

New Member
Joined
Jul 23, 2013
Messages
5
Hey everyone!

Brand new member here! I am running Excel for Mac OS X 2011 on Mac OS 10.8.

I'm working on a science experiment involving a lot of data with multiple trials. Here is a sample of raw values:


x1y1x2y2x3y3
51.42000.333450.72000.333451.12000.3334
52.74000.6667512000.333452.12000.3334
52.92000.333452.42000.333452.74000.6667
534000.666752.64000.666752.88001.3335
53.12000.333452.86001.000152.96001.0001
53.28001.333552.94000.6667532000.3334
53.32000.3334531.40E+0453.12000.3334
54.22000.333453.18001.333553.26001.0001
54.46001.000153.22000.333453.38001.3335
54.51.20E+0453.31.20E+0453.92000.3334
54.62.60E+0454.34000.666754.48001.3335
54.71.60E+0454.46001.000154.51.80E+04
54.82.60E+0454.53.20E+0454.62.80E+04
54.92.00E+0454.63.40E+0454.72.80E+04
551.80E+0454.73.40E+0454.85.20E+04
55.12.00E+0454.86.60E+0454.96.00E+04
55.23.80E+0454.97.20E+04554.00E+04
55.34.80E+04557.60E+0455.13.60E+04
55.41.60E+0455.15.60E+0455.25.20E+04
55.72000.333455.24.80E+0455.34.00E+04
55.82000.333455.38.40E+0455.41.40E+04
55.92000.333455.41.40E+0455.72000.3334
56.28001.333555.52000.333455.86001.0001
56.34000.666755.88001.333555.92000.3334
56.48.00E+04566001.0001562000.3334
56.52.12E+0556.14000.666756.14000.6667
56.63.68E+0556.24000.666756.21.00E+04
56.75.26E+0556.31.40E+0456.31.20E+04
56.86.42E+0556.46.00E+0456.48.40E+04
56.98.44E+0556.52.44E+0556.52.48E+05
578.26E+0556.64.70E+0556.63.44E+05

<tbody>
</tbody>

My x-values should increment by 0.1 but some values are skipped. I was wondering if someone can help me write a macro or formula to fill in the gaps and line up the data; that is, insert missing (x, y) pairs in each column wherever x-values skip by more than .1. For each inserted x-value, a corresponding y-value should be invented that takes the lesser of the established y-value above and below it. The effect is that all x-values would be the same in any given row whereas the y-values (whether native or invented) may vary.

I have taken the data above and done this manually to show an example of the output I am looking for:

x1y1x2y2x3y3
502000.3334
502000.3334
502000.3334
50.12000.3334
50.12000.3334
50.12000.3334
50.22000.3334
50.22000.3334
50.22000.3334
50.32000.3334
50.32000.3334
50.32000.3334
50.42000.3334
50.42000.3334
50.42000.3334
50.52000.3334
50.52000.3334
50.52000.3334
50.62000.3334
50.62000.3334
50.62000.3334
50.72000.3334
50.72000.333450.72000.3334
50.82000.3334
50.82000.3334
50.82000.3334
50.92000.3334
50.92000.3334
50.92000.3334
512000.3334
512000.3334512000.3334
51.12000.3334
51.12000.3334
51.12000.3334
51.22000.3334
51.22000.3334
51.22000.3334
51.32000.3334
51.32000.3334
51.32000.3334
51.42000.3334
51.42000.333451.42000.3334
51.52000.3334
51.52000.3334
51.52000.3334
51.62000.3334
51.62000.3334
51.62000.3334
51.72000.3334
51.72000.3334
51.72000.3334
51.82000.3334
51.82000.3334
51.82000.3334
51.92000.3334
51.92000.3334
51.92000.3334
522000.3334
522000.3334
522000.3334
52.12000.3334
52.12000.3334
52.12000.3334
52.22000.3334
52.22000.3334
52.22000.3334
52.32000.3334
52.32000.3334
52.32000.3334
52.42000.3334
52.42000.3334
52.42000.3334
52.52000.3334
52.52000.3334
52.52000.3334
52.62000.3334
52.64000.6667
52.62000.3334
52.74000.6667
52.74000.6667
52.74000.6667
52.82000.3334
52.86001.000152.88001.3335
52.92000.333452.94000.666752.96001.0001
534000.6667531.40E+04532000.3334
53.12000.333453.18001.333553.12000.3334
53.28001.333553.22000.333453.26001.0001
53.32000.333453.31.20E+0453.38001.3335
53.42000.3334
53.44000.6667
53.42000.3334
53.52000.3334
53.54000.6667
53.52000.3334
53.62000.3334
53.64000.6667
53.62000.3334
53.72000.3334
53.74000.6667
53.72000.3334
53.82000.3334
53.84000.6667
53.82000.3334
53.92000.3334
53.94000.6667
53.92000.3334
542000.3334
544000.6667
542000.3334
54.12000.3334
54.14000.6667
54.12000.3334
54.22000.333454.24000.6667
54.22000.3334
54.32000.3334
54.34000.666754.32000.3334
54.46001.000154.46001.000154.48001.3335
54.51.20E+0454.53.20E+0454.51.80E+04
54.62.60E+0454.63.40E+0454.62.80E+04
54.71.60E+0454.73.40E+0454.72.80E+04
54.82.60E+0454.86.60E+0454.85.20E+04
54.92.00E+0454.97.20E+0454.96.00E+04
551.80E+04557.60E+04554.00E+04
55.12.00E+0455.15.60E+0455.13.60E+04
55.23.80E+0455.24.80E+0455.25.20E+04
55.34.80E+0455.38.40E+0455.34.00E+04
55.41.60E+0455.41.40E+0455.41.40E+04
55.52000.3334
55.52000.3334
55.52000.3334
55.62000.3334
55.62000.3334
55.62000.3334
55.72000.3334
55.72000.3334
55.72000.3334
55.82000.333455.88001.333555.86001.0001
55.92000.3334
55.96001.0001
55.92000.3334
562000.3334
566001.0001562000.3334

<tbody>
</tbody>

The (x, y) values highlighted in red represent the invented values that the macro should insert. Notice how all x-values progress steadily by 0.1 increments, and consequently all unique x-values are aligned on the same row (e.g. all pairs where x = 55.9 are on the same row). Inserted y-values should be determined by taking the "low" of the y-value on either side (before/after), (e.g. x2 = 55.9 [inserted] corresponds to y2 = 6001.0001 [inserted] because this was the lesser of the y2-value before it (8001.3335) and the y2-value after it (6001.001). In a large gap, the y-values before and after the gap are compared, and the lesser of which is repeatedly inserted for every single y-value within the gap; meanwhile the corresponding x-values within the gap are inserted at 0.1 increments until the gap is patched. (This is seen on the 'x2' range from 53.4 to 54.2: the y2 values here all take on 4000.6667 because it is the lesser of the y2-value before the gap (12000) and y2-value after it (4000.6667)).

Unfortunately, it's unrealistically tedious to work through my entire dataset manually (hence the need to automate)! I don't know much about VBA or macros, so please bear with me and walk me through the steps if possible! I'm assuming I would need to customize the parameters of the macro to take into account all the rows and columns.

A final bit about those parameters: The entire dataset starts at 50.0. and ends at 650.0, and there are five (x, y) pairs (although only three are shown above). Also, I need to apply this macro to multiple worksheets.

I hope this explanation is clear and you understand what I'm trying to do. If not, please ask, and I will clarify with a follow-up!

Your help is so greatly appreciated. Thank you in advance!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Does anyone know if a macro like this is possible? Or would my best bet be to accomplish this the long way (manually)?

Please help! Even if you only have partial insight!
 
Upvote 0
This can be done with a macro. I would suggest since the data set runs from 50 to 650 that the macro first creates a new sheet with a single x column that goes from 50 to 650 in 0.1 increments. Then add 5 Y columns using the data from your raw data sheet where it matches the x value, or fabricating the y value if there is no such x value on the raw data sheet. Where there are sequential fabricated y values, only the first one needs to be determined from the minimum of the two y values that straddle the gap, the others will all be equal to this first one.

Perhaps you could provide more information on the exact layout of the raw data sheet .... what columns are the x-y pairs in and starting in what row, etc.? This will improve your chances of getting some help.
 
Upvote 0
Hey Joe,

Thanks for your reply!!

This can be done with a macro. I would suggest since the data set runs from 50 to 650 that the macro first creates a new sheet with a single x column that goes from 50 to 650 in 0.1 increments. Then add 5 Y columns using the data from your raw data sheet where it matches the x value,

I do actually have a spreadsheet that does exactly that: 1 x column with 5 y columns. The data in this spreadsheet is entered manually, but it would be great if a macro could do this for me (like you said) for my other workbooks.

or fabricating the y value if there is no such x value on the raw data sheet. Where there are sequential fabricated y values, only the first one needs to be determined from the minimum of the two y values that straddle the gap, the others will all be equal to this first one.

Yes, I agree. This is a great idea. Thank you!

Perhaps you could provide more information on the exact layout of the raw data sheet .... what columns are the x-y pairs in and starting in what row, etc.? This will improve your chances of getting some help.

In the raw data, the 5 x-y pairs reside in columns AB, DE, GH, JK, MN respectively. Row 1 is a header, Row 2 has the column names. The data begins on Row 3.

I have an example workbook where the first tab is my raw data (as just described), the second tab is my manual alignments, and the third tab is the manual alignments with the x column consolidated (as you suggested). The ultimate goal is to generate an overlay of these 5 graphs, if the macro could mimic the output of either Tab 2 or Tab 3, then accomplishing this graph would be a piece of cake.

I'd love to post my workbook so you can see what I'm talking about, but this forum doesn't allow attachments. (Am I able to PM the workbook instead?) It would be so much easier to show spreadsheet rather than describing it or trying to format a table in HTML for this message. (HTML Maker isn't compatible with my machine because I'm on a mac, and the copy/paste with borders method is really cumbersome and doesn't always render correctly.)

Again, I am running Excel for Mac 2011. (I'm not sure if macros can run interchangeably between a Windows and a Mac OS X machine, or if they need to be specifically designed for one or the other.)

Please let me know if you need further details or clarification. I am happy to provide! :)
 
Upvote 0
I don't use a Mac, but doubt that will cause any problems for what you want to do. If you will PM me, I will send you an email address where you can send a copy of your workbook and I'll try to come up with something for you.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,800
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top