Generate a list depending on date entered

Closed
shootingfish Posts 14 Registration date Sunday February 6, 2011 Status Member Last seen September 1, 2011 - Mar 17, 2011 at 06:53 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Mar 22, 2011 at 07:02 AM
Hello,

I'm very grateful in advance for any suggested solutions to this issue I'm having as I'm totally stuck and in need of a friendly prod in the right direction. I'm a fairly seasoned user of excel but this one just has me stumped.

I have provided an example of my workbook (a made up example with all the essential detail).

The problem is simply this, I have a set of names on a sheet and these people are registering their movements. In order to plan their holiday time i am trying to make a sheet - this will be called "Diary". There are a set of rules regarding holidays in order to maintain a certain skill set at all times. Unlike the example there are in fact 208 people in the business and these rules are essential. What is therefore needed is a way to quickly see if for instance a Manager is away on a specific date. This is shown in my example workbook on the "diary" tab.

Please help, i look forward to experimenting with new functions that i have never used before. Perhaps I will learn something as well as providing my office with a simple and convenient way of booking time off

http://www.4shared.com/document/bQXsVdWZ/Exampleofsheets.html


1 response

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Mar 18, 2011 at 10:19 AM
How familiar are you with macro?
0
shootingfish Posts 14 Registration date Sunday February 6, 2011 Status Member Last seen September 1, 2011 1
Mar 21, 2011 at 04:00 AM
I have a fair amount of experience. I just don't know the vocabulary until I have been shown :)
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Mar 21, 2011 at 10:49 AM
open VBE
Goto to Diary sheet

Paste this code


Private Sub Worksheet_Change(ByVal Target As Range) 

   If (Target.Address <> "$C$2") Then Exit Sub 
   If Target.Value = vbNullString Then Exit Sub 
    
   If IsDate(Target) Then 
      Application.EnableEvents = False 
      Call doLocateMovement(Target.Value) 
      Application.EnableEvents = True 
       
   Else 
      MsgBox "Please enter a valid date.", vbCritical, "Invalid Date" 
      Application.EnableEvents = False 
      Target = vbNullString 
      Application.EnableEvents = True 
   End If 
End Sub 

Private Sub doLocateMovement(sDate As String) 

   Dim lTgtRow               As Long 
   Dim rngTarget              As Range 
   Dim sMove                  As String 
   Dim sDiary                 As String 
   Dim iCol                   As Integer 
   Dim iMaxCol                As Integer 
   Dim iJobTitleRow           As Integer 
   Dim iNameRow               As Integer 
   Dim iRotationRow           As Integer 
   Dim dicJobTitle            As Object 
   Dim sJobTitle              As String 
   Dim sName                  As String 
   Dim sRotation              As String 
   Dim sReasonCode            As String 
   Dim vOption                As Variant 
   Dim arrRecGroup            As Variant 
   Dim arrRecDetail           As Variant 
   Dim lRecGroup              As Long 
   Dim lRecDetail             As Long 
   Dim lDetailRow             As Long 
    
   iJobTitleRow = 4 
   iNameRow = 5 
   iRotationRow = 6 
    
   sMoveSheet = "Movements" 
   sDiary = "Diary" 
   With Sheets(sMoveSheet) 

       ' locate the date text on column A
      lTgtRow = getItemRowLocation(sDate, .Name, Range("A:A")) 
      If (lTgtRow < 1) _ 
      Then 
         MsgBox sDate & " not found in sheet " & .Name 
         Exit Sub 
      End If 

     ' locate last column used on the row located
      iMaxCol = getLastColumn(.Name, Range(Cells(1, "B"), Cells(Rows.Count, Columns.Count))) 
      If (iMaxCol = 0) _ 
      Then 
         MsgBox "No record found for date " & sDate, vbInformation, "Record Not Found" 
         Exit Sub 
      End If 
       
      Set dicJobTitle = CreateObject("Scripting.Dictionary") 
      For iCol = 2 To iMaxCol 
         If (.Cells(lTgtRow, iCol) <> vbNullString) _ 
         Then 
            sJobTitle = .Cells(iJobTitleRow, iCol) 
            sName = .Cells(iNameRow, iCol) 
            sRotation = .Cells(iRotationRow, iCol) 
            sReasonCode = .Cells(lTgtRow, iCol) 
            If (dicJobTitle.Exists(sJobTitle)) _ 
            Then 
               dicJobTitle(sJobTitle) = dicJobTitle(sJobTitle) & "~" & sName & "|" & sRotation & "|" & sReasonCode 
            Else 
               dicJobTitle.Add Key:=sJobTitle, Item:=sName & "|" & sRotation & "|" & sReasonCode 
            End If 
         End If 
      Next 
   End With 
    
   With Sheets(sDiary) 
      lDetailRow = 8 
      .Range(.Cells(lDetailRow, 1), .Cells(.Rows.Count, .Columns.Count)).Clear 
       
      For Each vOption In dicJobTitle 
         sJobTitle = CStr(vOption) 
         .Cells(lDetailRow, "C") = sJobTitle 
          
         lDetailRow = lDetailRow + 1 
         arrRecGroup = Split(CStr(dicJobTitle(sJobTitle)), "~") 
         For lRecGroup = LBound(arrRecGroup) To UBound(arrRecGroup) 
            arrRecDetail = Split(arrRecGroup(lRecGroup), "|") 
             
               .Cells(lDetailRow, "C") = arrRecDetail(0) 
               .Cells(lDetailRow, "D") = arrRecDetail(1) 
               .Cells(lDetailRow, "E") = arrRecDetail(2) 
               lDetailRow = lDetailRow + 1 
         Next lRecGroup 
         lDetailRow = lDetailRow + 1 
      Next 
   End With 
    
End Sub 


Function getItemRowLocation(sLookFor As String, _ 
                            sSheetName As String, _ 
                            Optional myRange As Range = Nothing, _ 
                            Optional bFullString As Boolean = True, _ 
                            Optional bLastOccurance As Boolean = True) As Long 
' get last use row on the sheet 

   Dim Cell             As Range 
   Dim iLookAt          As Integer 
   Dim iSearchDir       As Integer 
  ' Dim rngFirst         As Range 
   Dim rngSearch        As Range 
    
   If (bFullString) _ 
   Then 
      iLookAt = xlWhole 
   Else 
      iLookAt = xlPart 
   End If 
    
   If (bLastOccurance) _ 
   Then 
      iSearchDir = xlPrevious 
   Else 
      iSearchDir = xlNext 
   End If 
    
   If myRange Is Nothing _ 
   Then 
      Set rngSearch = Sheets(sSheetName).Cells 
   Else 
      Set rngSearch = Sheets(sSheetName).Range(myRange.Address) 
   End If 
    
   With rngSearch 
      If (bLastOccurance) _ 
      Then 
         Set Cell = .Find(sLookFor, .Cells(1, 1), , iLookAt, xlByRows, iSearchDir) 
      Else 
         Set Cell = .Find(sLookFor, .Cells(.Rows.Count, .Columns.Count), , iLookAt, xlByRows, iSearchDir) 
      End If 
   End With 
    
   If Cell Is Nothing Then 
      getItemRowLocation = 0 
   Else 
      getItemRowLocation = Cell.Row 
   End If 
   Set Cell = Nothing 
   Set rngSearch = Nothing 
End Function 


Function getLastColumn(sSheetName As String, Optional myRange As Range = Nothing) As Long 
' get last use row on the sheet 

   Dim Cell      As Range 
    
   If (myRange Is Nothing) _ 
   Then 
      Set Cell = Sheets(sSheetName).Cells.Find("*", Cells(1, 1), , , xlByColumns, xlPrevious) 
   Else 
      Set Cell = Sheets(sSheetName).Range(myRange.Address).Find("*", Sheets(sSheetName).Range(myRange.Address).Cells(1, 1), , , xlByColumns, xlPrevious) 
   End If 
   If Cell Is Nothing Then 
      getLastColumn = 0 
   Else 
      getLastColumn = Cell.Column 
   End If 
   Set Cell = Nothing 

End Function
0
shootingfish Posts 14 Registration date Sunday February 6, 2011 Status Member Last seen September 1, 2011 1
Mar 21, 2011 at 11:48 AM
Wow, thats some impressive code and thanks for helping me out. I just tried it on the example sheet i posted though and it terminates at this part (in other words it gives me the "01/01/2011 not found in sheet Movements" msgbox)

With Sheets(sMoveSheet)

lTgtRow = getItemRowLocation(sDate, .Name, Range("A:A"))
If (lTgtRow < 1) _
Then
MsgBox sDate & " not found in sheet " & .Name
Exit Sub
End If

I thought it might be the date format i was entering but after pasting the values from the movements sheet and altering the cell formats about i had no joy.

Also, FYI, i can barely make out what your VBA code is talking about so i guess i'm not fairly experienced after all!!! A massive learning opportunity though
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Mar 21, 2011 at 02:19 PM
have the date format as m/d/yyyy in movement sheet and search in diary as same.

The basic idea of the code is that
1. locate on the movement sheet, the date.
2. Once row row is found, start moving from column to column on that row to see if there is any text.
3. if a text is found that record, then check if we have seen this job title before while scanning the row. if we have not seen this job title before, then for that job title record the name and rotation. If we have seen it, then add the name and rotation to previous entries of the job title.
4. once all the columns have been scanned, start outputing for each job titles, the names and rotations,

https://authentification.site/files/27531363/Exampleofsheets.xls
0
shootingfish Posts 14 Registration date Sunday February 6, 2011 Status Member Last seen September 1, 2011 1
Mar 22, 2011 at 06:29 AM
Sorry about this but changing the format of the cells doesn't seem to work. I even downloaded the one you linked to and it still comes up with the date not found msgbox
0