Divide and Conquer

clarka9

New Member
Joined
Mar 21, 2013
Messages
28
I am trying to automate a sheet that will be updated continously as I move on, but I will need the code to break the ranges out for each on the next sheet.
Thanks ALOT!!!!!!!......

First Name</SPAN>Last</SPAN>Assign Begin Dt</SPAN>Assign Retn Dt</SPAN>
Anthony</SPAN>Teft</SPAN>1/7/2013</SPAN>5/31/2013</SPAN>
John</SPAN>Mahoney</SPAN>12/31/2014</SPAN>
Adam</SPAN>Sweet</SPAN>10/13/2008</SPAN>12/31/2013</SPAN>
Gregory</SPAN>Klock</SPAN>3/11/2013</SPAN>3/20/2013</SPAN>
Kevin</SPAN>Sauve</SPAN>3/11/2013</SPAN>3/24/2013</SPAN>
Paul</SPAN>Barnes</SPAN>12/3/2012</SPAN>12/23/2012</SPAN>
Matthew</SPAN>Seibert</SPAN>3/11/2013</SPAN>3/24/2013</SPAN>

<TBODY>
</TBODY>
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
clarka9,

If we start with this in worksheet Data:


Excel 2007
ABCD
1First NameLastAssign Begin DtAssign Retn Dt
2AnthonyTeft1/7/20135/31/2013
3JohnMahoney12/31/2014
4AdamSweet10/13/200812/31/2013
5GregoryKlock3/11/20133/20/2013
6KevinSauve3/11/20133/24/2013
7PaulBarnes12/3/201212/23/2012
8MatthewSeibert3/11/20133/24/2013
9
Data


I did not show all 63 rows for Adam Sweet. Is this what worksheet Results should look like?


Excel 2007
ABCD
1First NameLastDateLevel
2AnthonyTeft1/7/20135
3AnthonyTeft2/7/20135
4AnthonyTeft3/7/20135
5AnthonyTeft4/7/20135
6AnthonyTeft5/31/20135
7JohnMahoney12/31/20141
8AdamSweet10/13/200863
70AdamSweet12/31/201363
71GregoryKlock3/11/20132
72GregoryKlock3/20/20132
73KevinSauve3/11/20132
74KevinSauve3/24/20132
75PaulBarnes12/3/20122
76PaulBarnes12/23/20122
77MatthewSeibert3/11/20132
78MatthewSeibert3/24/20132
79
Results
 
Upvote 0
clarka9,

I did not write any code yet. I just wanted to make sure that we understand each other.

I should be back later this evening.
 
Upvote 0
Reminder, this sheet will continue to be populated as I move on. Will the code recognize that?
Thanks!


clarka9,

I did not write any code yet. I just wanted to make sure that we understand each other.

I should be back later this evening.
 
Upvote 0
clarka9,

Sample raw data in worksheet Data:


Excel 2007
ABCD
1First NameLastAssign Begin DtAssign Retn Dt
2AnthonyTeft1/7/20135/31/2013
3JohnMahoney12/31/2014
4AdamSweet10/13/200812/31/2013
5GregoryKlock3/11/20133/20/2013
6KevinSauve3/11/20133/24/2013
7PaulBarnes12/3/201212/23/2012
8MatthewSeibert3/11/20133/24/2013
9
Data


After the macro in a new worksheet Results (some rows are hidden for brevity):


Excel 2007
ABCD
1First NameLastDateLevel
2AnthonyTeft1/7/20135
3AnthonyTeft2/7/20135
4AnthonyTeft3/7/20135
5AnthonyTeft4/7/20135
6AnthonyTeft5/31/20135
7JohnMahoney12/31/20141
8AdamSweet10/13/200863
10AdamSweet12/13/200863
11AdamSweet1/13/200963
22AdamSweet12/13/200963
23AdamSweet1/13/201063
34AdamSweet12/13/201063
35AdamSweet1/13/201163
46AdamSweet12/13/201163
47AdamSweet1/13/201263
58AdamSweet12/13/201263
59AdamSweet1/13/201363
70AdamSweet12/13/201363
71GregoryKlock3/11/20132
72GregoryKlock3/20/20132
73KevinSauve3/11/20132
74KevinSauve3/24/20132
75PaulBarnes12/3/20122
76PaulBarnes12/23/20122
77MatthewSeibert3/11/20132
78MatthewSeibert3/24/20132
79
Results


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub clarka9()
' hiker95, 03/23/2013
' http://www.mrexcel.com/forum/excel-questions/692848-divide-conquer-2.html
Dim wD As Worksheet, wR As Worksheet
Dim r As Long, lr As Long, sr As Long, er As Long, nr As Long
Dim lrra As Long, lrrd As Long, n As Long
Dim m As Long, sm As Long, em As Long
Dim y As Long, sy As Long, ey As Long
Application.ScreenUpdating = False
Set wD = Worksheets("Data")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=wD).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
With wR.Cells(1, 1).Resize(, 4)
  .Value = [{"First Name","Last","Date","Level"}]
  .Font.Bold = True
End With
lr = wD.Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr Step 1
  If wD.Cells(r, 3) = "" Or wD.Cells(r, 4) = "" Then
    nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wR.Cells(nr, 1).Resize(, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
    If wD.Cells(r, 3) = "" Then
      wR.Cells(nr, 3) = wD.Cells(r, 4)
      wR.Cells(nr, 4) = 1
    ElseIf wD.Cells(r, 4) = "" Then
      wR.Cells(nr, 3) = wD.Cells(r, 3)
      wR.Cells(nr, 4) = 1
    End If
  ElseIf Year(wD.Cells(r, 3)) = Year(wD.Cells(r, 4)) And Month(wD.Cells(r, 3)) = Month(wD.Cells(r, 4)) Then
    nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wR.Cells(nr, 1).Resize(2, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
    wR.Cells(nr, 3) = wD.Cells(r, 3)
    wR.Cells(nr, 4).Resize(2) = 2
    wR.Cells(nr + 1, 3) = wD.Cells(r, 4)
  ElseIf Year(wD.Cells(r, 3)) = Year(wD.Cells(r, 4)) And Month(wD.Cells(r, 3)) <> Month(wD.Cells(r, 4)) Then
    nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    m = Month(wD.Cells(r, 4)) - Month(wD.Cells(r, 3))
    wR.Cells(nr, 1).Resize(m + 1, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
    wR.Cells(nr, 3) = wD.Cells(r, 3)
    With wR.Cells(nr + 1, 3).Resize(m - 1)
      .FormulaR1C1 = "=MONTH(R[-1]C)+1&""/""&DAY(R[-1]C)&""/""&YEAR(R[-1]C)"
      .Value = .Value
    End With
    wR.Cells(nr + m, 3) = wD.Cells(r, 4)
    wR.Cells(nr, 4).Resize(m + 1).Value = m + 1
  ElseIf Year(wD.Cells(r, 3)) <> Year(wD.Cells(r, 4)) Then
    sm = Month(wD.Cells(r, 3))
    sy = Year(wD.Cells(r, 3))
    em = Month(wD.Cells(r, 4))
    ey = Year(wD.Cells(r, 4))
    For y = sy To ey Step 1
      If y = sy Then
        nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wR.Cells(nr, 1).Resize(, 3).Value = wD.Cells(r, 1).Resize(, 3).Value
        For m = sm + 1 To 12 Step 1
          nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
          wR.Cells(nr, 1).Resize(, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
          With wR.Cells(nr, 3)
            .FormulaR1C1 = "=MONTH(R[-1]C)+1&""/""&DAY(R[-1]C)&""/""&YEAR(R[-1]C)"
            .Value = .Value
          End With
        Next m
      ElseIf y > sr And y < ey Then
        nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wR.Cells(nr, 1).Resize(, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
        With wR.Cells(nr, 3)
          .FormulaR1C1 = "=MONTH(1)&""/""&DAY(R[-1]C)&""/""&YEAR(R[-1]C)+1"
          .Value = .Value
        End With
        For m = 2 To 12 Step 1
          nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
          wR.Cells(nr, 1).Resize(, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
          With wR.Cells(nr, 3)
            .FormulaR1C1 = "=MONTH(R[-1]C)+1&""/""&DAY(R[-1]C)&""/""&YEAR(R[-1]C)"
            .Value = .Value
          End With
        Next m
      ElseIf y = ey Then
        nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wR.Cells(nr, 1).Resize(, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
        With wR.Cells(nr, 3)
          .FormulaR1C1 = "=MONTH(1)&""/""&DAY(R[-1]C)&""/""&YEAR(R[-1]C)+1"
          .Value = .Value
        End With
        For m = 2 To em Step 1
          nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
          wR.Cells(nr, 1).Resize(, 2).Value = wD.Cells(r, 1).Resize(, 2).Value
          With wR.Cells(nr, 3)
            .FormulaR1C1 = "=MONTH(R[-1]C)+1&""/""&DAY(R[-1]C)&""/""&YEAR(R[-1]C)"
            .Value = .Value
          End With
        Next m
      End If
    Next y
    lrra = wR.Cells(Rows.Count, 1).End(xlUp).Row
    lrrd = wR.Cells(Rows.Count, 4).End(xlUp).Row
    n = lrra - lrrd
    wR.Range("D" & lrrd + 1 & ":D" & lrra) = n
  End If
Next r
lr = wR.Cells(Rows.Count, 1).End(xlUp).Row
wR.Range("C2:C" & lr).NumberFormat = "m/d/yyyy"
wR.Cells.EntireColumn.AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the clarka9 macro.


Reminder, this sheet will continue to be populated as I move on. Will the code recognize that?

Each time you run the macro worksheet Results will be cleared, and, then all the raw data in worksheet Data will be processed into worksheet Results.
 
Upvote 0
I am getting a massage stating that something is interfering in this massage is wrong "ElseIf Year(wD.Cells(r, 3)) = Year(wD.Cells(r, 4)) And Month(wD.Cells(r, 3)) = Month(wD.Cells(r, 4)) Then".

It says "Runtime Error '13'
Type Mismatch
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,767
Messages
6,132,599
Members
449,738
Latest member
brianm3y3r

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