Macro for Quick Formatting

muell595

New Member
Joined
Aug 6, 2013
Messages
2
Good afternoon, everyone.

Could I get some help on writing a macro? Starting in R1C1, I am given the following from an external database following:

Name
List1
List2
List3
List4
List5
List6
List7
Useless Text
9483
37.4
4.3
19
23.7
12.5
3.7
Last, First
62
0.9
4.5
085.5
6.8
0.1
7/1/2013
2
1.1
5.5
093
00.3
07:00
0
0
0
0
100
0
0
07:15
0
0
0
0
100
0
0
07:30
0
0
0
0
100
0
0
07:45
0
0
0
0
100
0
0
08:00
0
0
0
0
100
0
0
08:15
0
0
0
0
100
0
0
08:30
0
0
0
0
100
0
0
08:45
0
0
0
0
100
0
0
09:00
0
0
15.1
0
84.8
0
0
09:15
0
0
0
0
100
0
0
7/2/2013
2
1.8
77.5
14.3
07:00
0
0
0
0
26.5
73.3
0
07:15
0
0
0
0
99.8
0.2
0
07:30
0
0
0
0
100
0
0
07:45
0
0
0
0
100
0
0
08:00
0
0
0
0
100
0
0
08:15
0
0
0
0
100
0
0
08:30
0
0
0
0
100
0
0
08:45
0
0
0
0
100
0
0
09:00
0
0
7.1
0
91.8
0
0
09:15
0
0
0
0
100
0
0
Last2,First2
3
2
2.3
86
14
07:00
0
0
0
0
100
0
0
07:15
0
0
0
0
99.9
0.1
0
07:30
0
0
0
0
100
0
0
07:45
1
0
0
0
98.1
0
0
08:00
0
0
0
0
100
0
0
08:15
0
0
0
0
100
0
0
08:30
0
0
0
0
100
0
0
08:45
0
0
0
0
100
0
0
09:00
0
0
4
0
94.3
0
0
09:15
0
0
2.6
0
97.4
0
0
09:30
0
0
0
0
100
0
0

<tbody>
</tbody>


I would like it to look more like the following below. Considering the actual list is hundreds of times longer than this, a macro would be nice. The colored texts in the tables above and below are actually filled with that color with black text. Essentially I would like to have the name added in a separate column left of the time, and get rid of all the crap cells to mimic this table:

Name
Time
List1
List 2
List3
List 4
List 5
List 6
List 7
Last, First 7/1/2013
07:00
100
Last, First 7/1/2013
07:15
100
Last, First 7/1/2013
07:30
100
Last, First 7/1/2013
07:45
100
Last, First 7/1/2013
08:00
100
Last, First 7/1/2013
08:15
100
Last, First 7/1/2013
08:30
100
Last, First 7/1/2013
08:45
100
Last, First 7/1/2013
09:00
15.1
84.8
Last, First 7/1/2013
09:15
100
Last, First 7/2/2013
07:00
26.5
73.3
Last, First 7/2/2013
07:15
99.8
0.2
Last, First 7/2/2013
07:30
100
Last, First 7/2/2013
07:45
100
Last, First 7/2/2013
08:00
100
Last, First 7/2/2013
08:15
100
Last, First 7/2/2013
08:30
100
Last, First 7/2/2013
08:45
100
Last, First 7/2/2013
09:00
7.1
91.8
Last, First 7/2/2013
09:15
100
Last2,First2 7/1/2013
07:00
100
Last2,First2 7/1/2013
07:15
99.9
0.1
Last2,First2 7/1/2013
07:30
100
Last2,First2 7/1/2013
07:45
1
98.1
Last2,First2 7/1/2013
08:00
100
Last2,First2 7/1/2013
08:15
100
Last2,First2 7/1/2013
08:30
100
Last2,First2 7/1/2013
08:45
100
Last2,First2 7/1/2013
09:00
4
94.3
Last2,First2 7/1/2013
09:15
2.6
97.4

<tbody>
</tbody>

I don't even care about the fill or anything of the like either. Is there someone out there willing to be a hero today and help me? It would be greatly appreciated!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
How do I know that the first entries for Last2,First2 are starting on 7/1/2013? In the macro below I have assumed a new section (unless immediately followed by date) will start at the very first date in the list.

I also do not know if the dates and times are imported as text or as numbers, so i have taken both options into account.

Because we work totally in memory, only one read from the sheet, and one write to the sheet, the macro is very fast

Enjoy.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> ResortList()<br>    <SPAN style="color:#00007F">Dim</SPAN> rOut <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> aIn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, aOut <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> lRin <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lRout <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> sSect <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sDate <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#007F00">'We are going to do all the rehashing of _<br>    first two columns and deleting of zeros _<br>    in memory, rather than looping on the sheet. _<br>    For this we work with two arrays, aIn and aOut</SPAN><br>    <br>    <SPAN style="color:#007F00">'fill aIn array with the input data table</SPAN><br>    aIn = Cells(1, 1).CurrentRegion<br>    <br>    <SPAN style="color:#007F00">'the array is now just like a sheet with _<br>     'rows' and 'columns'. Now we need to size _<br>     the output array to be same number of rows _<br>     and one column wider. In fact it will give _<br>     us few too many rows, but that doesn't matter</SPAN><br>    <SPAN style="color:#00007F">ReDim</SPAN> aOut(1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aIn, 1), 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aIn, 2) + 1)<br>    <br>    <SPAN style="color:#007F00">'Set title row</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aIn, 2)<br>        aOut(1, i + 1) = aIn(1, i)<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    aOut(1, 1) = aIn(1, 1)<br>    aOut(1, 2) = "Time"<br>    lRout = 2<br><br>    <SPAN style="color:#007F00">'now process rest of aIn. Start on row 3</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> lRin = 3 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aIn, 1)<br>        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">True</SPAN><br>            <SPAN style="color:#00007F">Case</SPAN> Left(aIn(lRin, 1), 4) = "Last"<br>                <SPAN style="color:#007F00">'signals 'new section'</SPAN><br>                sSect = aIn(lRin, 1)<br>                aOut(lRout, 1) = sSect & " - " & sDate<br>            <br>            <SPAN style="color:#00007F">Case</SPAN> Mid(aIn(lRin, 1), 3, 1) = "/", _<br>                IsDate(aIn(lRin, 1))<br>                <SPAN style="color:#007F00">'signals new date</SPAN><br>                <SPAN style="color:#007F00">'if first date, store for start _<br>                 new sections</SPAN><br>                <SPAN style="color:#007F00">'in above case i check for both numerical _<br>                 date as for text date format</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> sDate = vbNullString <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#00007F">If</SPAN> IsDate(aIn(lRin, 1)) <SPAN style="color:#00007F">Then</SPAN><br>                        sDate = Format(aIn(lRin, 1), "MM/D/YYYY")<br>                    <SPAN style="color:#00007F">Else</SPAN><br>                        sDate = aIn(lRin, 1)<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#007F00">'Put section & date in first column</SPAN><br>                aOut(lRout, 1) = sSect & " - " & aIn(lRin, 1)<br>                <br>            <SPAN style="color:#00007F">Case</SPAN> Mid(aIn(lRin, 1), 3, 1) = ":", IsNumeric(aIn(lRin, 1))<br>                <SPAN style="color:#007F00">' signals a time. copy List columns</SPAN><br>                <SPAN style="color:#007F00">'case accounts for both text time as for _<br>                 numerical time (fraction)</SPAN><br>                <SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aOut, 2)<br>                    <SPAN style="color:#00007F">If</SPAN> aIn(lRin, i - 1) <> 0 <SPAN style="color:#00007F">Then</SPAN><br>                        aOut(lRout, i) = aIn(lRin, i - 1)<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Next</SPAN> i<br>                <SPAN style="color:#00007F">If</SPAN> aOut(lRout, 1) = vbNullString <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#007F00">'copy 'cell' above</SPAN><br>                    aOut(lRout, 1) = aOut(lRout - 1, 1)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                lRout = lRout + 1<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> lRin<br><br>    <SPAN style="color:#007F00">'output array to new sheet</SPAN><br>    Sheets.Add after:=ActiveSheet<br>   <SPAN style="color:#007F00">' ActiveSheet.Name = "List"</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rOut = Cells(1, 1) <SPAN style="color:#007F00">'set to A1</SPAN><br>    <SPAN style="color:#007F00">'write data from aOut to range sized to same size</SPAN><br>    rOut.Resize(UBound(aOut, 1), UBound(aOut, 2)).Value = aOut<br>    <br>    Columns(1).EntireColumn.AutoFit<br>    Columns(2).EntireColumn.NumberFormat = "HH:MM"<br>    <br>    <SPAN style="color:#007F00">'clean up</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rOut = <SPAN style="color:#00007F">Nothing</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br></FONT>
 
Upvote 0
I have a feeling this is going to work extremely well, but I am now getting an error with the number of arguments for line:
sDate = Format(aIn(lRin, 1), "MM/D/YYYY")

Is that becasue it's the wrong format? Honestly, it'll take me some time to wrap my head around this beautiful piece of work you wrote, so at this moment I'm sitting here with my head sideways, drooling. Do you know the quick answer for this one?

The dates and times are all imported as text/"General." I have a feeling that's they key here, but I don't really know for sure.
 
Upvote 0
Mmhh, although the format should be "MM/DD/YYYY" in your case this should not a difference


I suggest you do a bit of debugging: open the vba editor, go to the macro and click in the border left of this line. A red dot should appear and the line should turn red. You have set a breakpoint, the code will stop here every time it runs. So run the macro. The editor will show with the red line marked yellow. Hold your mouse over the aIn(lRin,1). It should show you the date. Press F8 key to execute the next step. If it executes without an error, then press F5 to continue run the code. It will stop at the same line again. Do this until it gives the error. What is the value of aIn(lRin,1) ?


I do not have access to Excel at the moment, and so can't check, but possibly the format function can't operate if the date is text and not a value. I will then have to rethink how to modify this piece of code.


but let me know the results of the debugging.
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,844
Members
449,411
Latest member
adunn_23

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