0

Here is an example table I am trying to populate:

Item   |   Date   |   Assigned Date
------------------------------------
 1     |   Apr 1  |     
 2     |   Apr 1  |     
 3     |   Apr 1  |     
 4     |   Apr 2  |     
 5     |   Apr 2  |     
 6     |   Apr 2  |     
 7     |   Apr 2  |     
 8     |   Apr 3  |     
 9     |   Apr 4  |     
 10    |   Apr 4  |         

Base on this table

Date   |   Max Allowed   |   Current Count
----------------------------------------------
Apr 1  |      2          |      
Apr 2  |      4          |
Apr 3  |      1          |
Apr 4  |      2          |
Apr 5  |      2          |

I am trying to figure out a way to create values base on the data in the working column. I know this will give me circular references and do not know of a way to accomplish this. Right now I turned on iterative calculation, and do a countif against (Assigned Date) to see if I should use the first available date, or increment it until it follows the second table. However, the countif is not working properly with iterative calculations.

Would I need to use VBA? So at the end of the day, the two tables would need to be:

Item   |   Date   |   Assigned Date
------------------------------------
 1     |   Apr 1  |     Apr 1
 2     |   Apr 1  |     Apr 1     
 3     |   Apr 1  |     Apr 2     
 4     |   Apr 2  |     Apr 2     
 5     |   Apr 2  |     Apr 2     
 6     |   Apr 2  |     Apr 2     
 7     |   Apr 2  |     Apr 3     
 8     |   Apr 3  |     Apr 4     
 9     |   Apr 4  |     Apr 4     
 10    |   Apr 4  |     Apr 5         
Date   |   Max Allowed   |   Current Count
----------------------------------------------
Apr 1  |      2          |       2
Apr 2  |      4          |       4
Apr 3  |      1          |       1
Apr 4  |      2          |       2
Apr 5  |      2          |       1

If VBA is needed, can you please direct me to some resources or code examples that would help me with accomplishing this? Thank you very much in advance.

1
  • hey @DON'TPANIC, if my solution below worked I'd appreciate you marking it as correct. Thanks! -Dan
    – Dan Wagner
    Apr 3, 2014 at 10:51

1 Answer 1

0

I don't think you could accomplish this task without VBA. Here is how I would populate the tables (assuming a setup based on the picture below):

tables

Option Explicit
Sub PopulateTables()

'declare variables
Dim DataSheet As Worksheet, ControlSheet As Worksheet
Dim LastDataRow As Long, LastControlRow As Long, DataIdx As Long, _
    CurrentCount As Long, MaxAllowed As Long, _
    ControlDateCol As Long, DataDateCol As Long, _
    ControlMaxAllowedCol As Long, ControlCurrentCountCol As Long, _
    DataItemCol As Long, DataAssignedDateCol As Long, _
    ControlStartRow As Long
Dim DataDate As Date, ControlDate As Date
Dim ControlDateRange As Range, ControlStartRange As Range, _
    DataAssignedDateRange As Range

'assign sheets for easy reference, initialize location variables
Set DataSheet = ThisWorkbook.Worksheets("data")
Set ControlSheet = ThisWorkbook.Worksheets("control")
ControlDateCol = 1
ControlMaxAllowedCol = 2
ControlCurrentCountCol = 3
DataItemCol = 1
DataDateCol = 2
DataAssignedDateCol = 3


'identify the last rows of the sheets
LastDataRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastControlRow = ControlSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

'identify the control date range
Set ControlDateRange = Range(ControlSheet.Cells(2, ControlDateCol), ControlSheet.Cells(LastControlRow, ControlDateCol))

'loop through the data table
For DataIdx = 2 To LastDataRow

    DataDate = DataSheet.Cells(DataIdx, DataDateCol)
    Set ControlStartRange = ControlDateRange.Find(DataDate, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    'error trap - if date isn't found, error out
    If ControlStartRange Is Nothing Then
        MsgBox ("Date in row " & DataIdx & " not found on control sheet, exiting!")
        Exit Sub
    End If

    'start the counting of rows
    ControlStartRow = ControlStartRange.Row

'the "restart" point
AssignCountAndMaxValues:

    'assign the count and max allowed values
    CurrentCount = ControlSheet.Cells(ControlStartRow, ControlCurrentCountCol).Value
    MaxAllowed = ControlSheet.Cells(ControlStartRow, ControlMaxAllowedCol).Value

    'decision logic
    Select Case CurrentCount
    'something weird happened, the count somehow became higher than the max
    Case Is > MaxAllowed
        MsgBox ("An error has occurred, current count is greater than max allowed. Exiting...")
        Exit Sub
    'have not hit the maximum allowable number of dates for this row
    Case Is < MaxAllowed
        DataSheet.Cells(DataIdx, DataAssignedDateCol) = ControlSheet.Cells(ControlStartRow, ControlDateCol)
        CurrentCount = CurrentCount + 1
        ControlSheet.Cells(ControlStartRow, ControlCurrentCountCol).Value = CurrentCount
    'maximum number of dates for this row has been reached, try the next row
    Case Else
        ControlStartRow = ControlStartRow + 1
        GoTo AssignCountAndMaxValues
    End Select

    'error trap - run out of control dates to compare against
    If ControlStartRow > LastControlRow Then
        MsgBox ("Error, control sheet is out of dates to compare. Exiting...")
        Exit Sub
    End If

Next DataIdx

'format the Excel-numerical dates as short dates
Set DataAssignedDateRange = Range(DataSheet.Cells(2, DataAssignedDateCol), DataSheet.Cells(LastDataRow, DataAssignedDateCol))
DataAssignedDateRange.NumberFormat = "m/d/yyyy"

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.