VBA Code to compare single column date on worksheet with 2 column start and end dates, copy select information horizontally, to empty cells.

GAdams106

New Member
Joined
Feb 10, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to work assignments based off of requests for work. i have some VBA Experience, but everything I've tried is falling to pieces, and i wanted to see if i could get some help.
I gathered all time off requests for a timeframe (April 1 - Sept 30) in a forms and exported it to a worksheet. so it's : Name, 1st choice start, 1st choice end, BLANK, 2nd choice start, 2nd end, etc all the way to 5th choice.
On the list worksheet. i have dates listed vertically from April 1 all the way to sept 30. what i have been trying to do, is have VBA check the date in list (example: 04/01/2022) and compare it to the date ranges in all the requests on master. If a person requested time off for that day (say 04/01/-04/07) then i want it to list their last name, first name, id # (on master, G: ID # ,H: last name, I: First name) in one cell on the list worksheet row with the date. Then the next person, then the next, so that i end up with a horizontal list of every person who requested to be off on that particular day.
Examples are below. I have tried implementing multiple different codes from the forums, and none of them are getting me anywhere, so I'm absolutely up to starting from scratch.

End goal, is to have 5 buttons on the list page. 1 for each choice. 1st choice populates in 1 color, i compare the number of personnel authorized to be off that day to how many requested. If there's room for more people, press second choice, and it populates more in a different color. continuing horizontally.

I'm trying to figure this out in the macro recorder, but if anyone could help, i would truly appreciate it.

Mini Sheet Example-No PII.xlsm
ABCDEFGHIJKLM
1ID #Last Name:First Name:1st Choice Vacation Beginning Date:1st Choice Vacation End Date:2nd Choice Vacation Beginning Date:2nd Choice Vacation End Date:3rd Choice Vacation Beginning Date:3rd Choice Vacation End Date:4th Choice Vacation Beginning Date:4th Choice Vacation End Date:5th Choice Vacation Beginning Date:5th Choice Vacation End Date:
21SMITHJOHN4/17/20225/1/20227/17/20227/24/20226/19/20226/26/20227/3/20227/10/20229/4/20229/11/2022
32JACKSONMICHAEL7/2/20227/8/20225/25/20226/1/20224/13/20224/20/20229/1/20229/7/20226/15/20226/22/2022
43JOHNSONPAUL4/22/20225/2/20224/1/20224/11/20227/24/20228/1/20226/10/20226/17/20229/16/20229/23/2022
54POWERSAUSTIN7/11/20227/18/20229/19/20229/26/20227/18/20227/25/2022
65DIGGLERDIRK4/13/20224/19/20226/21/20227/6/20227/12/20227/26/20228/7/20228/14/20229/7/20229/14/2022
7
8
9
10
11
12
13
14
15
16
17
MASTER


Mini Sheet Example-No PII.xlsm
ABCDEFGHI
1
2People Off: Total Personnel: (337) 50.55 Is 15%DATEFIRST CHOICE (WILL BE A BUTTON)SECOND CHOICE (WILL BE A BUTTON)THIRD CHOICE (WILL BE A BUTTON)FOURTH CHOICE (WILL BE A BUTTON)FIFTH CHOICE (WILL BE A BUTTON)
324.0%4/1/2022Smith, John ID1Johnson, Paul, ID 3
44/2/2022Johnson, Paul, ID 3Diggler, Dirk ID5Powers, Austin ID 4
54/3/2022Jackson, Michael ID 4
64/4/2022
74/5/2022Johnson, Paul, ID 3Diggler, Dirk ID5Powers, Austin ID 4Smith, John ID1
84/6/2022Jackson, Michael ID 4Johnson, Paul, ID 3Diggler, Dirk ID5Powers, Austin ID 4
94/7/2022
104/8/2022
114/9/2022
124/10/2022
134/11/2022
144/12/2022
154/13/2022
164/14/2022
174/15/2022
184/16/2022
194/17/2022
204/18/2022
List
Cell Formulas
RangeFormula
B2B2=" Total Personnel: (337) " & 15%*(337) & " Is 15%"
A3A3=SUMPRODUCT((D3:AS3<>"")*1)
B3B3=A3/50.55


Mini Sheet Example-No PII.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1ID NAME1stENDGranted2ndEndGranted3rdEndGranted4th EndGranted5th EndGranted
21/1/1900SMITHJOHN4/17/20225/1/2022YES7/17/20227/24/2022YES6/19/20226/26/2022NO7/3/20227/10/20229/4/20229/11/2022
31/2/1900JACKSONMICHAEL7/2/20227/8/2022YES5/25/20226/1/2022NO4/13/20224/20/2022NO9/1/20229/7/20226/15/20226/22/2022
41/3/1900JOHNSONPAUL4/22/20225/2/2022YES4/1/20224/11/2022NO7/24/20228/1/2022NO6/10/20226/17/20229/16/20229/23/2022
51/4/1900POWERSAUSTIN7/11/20227/18/2022YES9/19/20229/26/2022YES7/18/20227/25/2022NO
61/5/1900DIGGLERDIRK4/13/20224/19/2022YES6/21/20227/6/2022NO7/12/20227/26/2022NO8/7/20228/14/20229/7/20229/14/2022
Results
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
So,
This is where i've gotten to so far, i've pieced together a few different codes and some macro work and i'm stuck on what i "believe" to be the final part:
VBA Code:
Sub Fix_()
Dim startdate As Date, enddate As Date
Dim rng As Range, destRow As Long
Dim shtSrc As Worksheet, shtDest As Worksheet
Dim c As Range
Dim ws As Worksheet


    Set shtSrc = Sheets("List")
    Set shtDest = Sheets("Master")
    destRow = 3 'start copying to this row

    startdate = CDate(shtDest.Cells("D").End(xlDown))
    enddate = CDate(shtDest.Cells("E").End(xlDown))

    
    Set rng = Application.Intersect(shtSrc.Range("C"), shtSrc.UsedRange)

    For Each c In rng.Cells
        If c.Value >= startdate And c.Value <= enddate Then
    startdate.Offset(0, -1).Copy
    shtSrc.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteFormulas


            destRow = destRow + 1

        End If
    Next

End Sub
In pieces everything works, however now, i can't get the code to accept the startdate.offset(0, -1).copy.
i need it to copy the date . Anyone have any ideas?


@Fluff 90% of what i know in VBA has come from your posts, was curious if you had any ideas?
 
Upvote 0
Okay Folks,
Not gonna lie, feeling pretty great as a 1 man army, but i'm so close! scratched everything, got to here. everything seems to be lining up. but i'm still not getting my pasted data. any ideas?

VBA Code:
Sub CopyDataUsingDateRange()

    Application.ScreenUpdating = False

    Dim wsData As Worksheet, wsDate As Worksheet
    Dim dSDate As Date, dEDate As Date
    Dim LastRow As Long
    Dim rCell As Range
    Dim Cel As Range
        
    'set the worksheet objects
    Set wsData = ThisWorkbook.Sheets("Master")
    Set wsDate = ThisWorkbook.Sheets("List")
    

    'required variables
    dSData = wsData.Range("D2").End(xlUp).Value
    dEData = wsData.Range("E2").End(xlUp).Value
    

   With Sheets("Trial")
   LastRow = Range("C3").End(xlDown).Row
   Set rCell = Range("C3" & LastRow)
    For Each Cel In rCell
        If IsDate(Cel) Then
                With wsData 
                If Cel >= dSData And rCel <= dEData Then
                    wsData.Cells.Offset(, -5).Value.Copy
                    wsDate.Cells(, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteFormulas
                    Application.CutCopyMode = False
                End If
                End With
        End If
       
    Next Cel
   End With
            
   Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
I'm really struggling at the moment. I am running through the debugger, and i can't get it to paste. would seriously love some help because i know it's something simple.

Everything i've been trying to do refuses to let me paste to the next available column. i keeeeeep trying. i just need it to paste on the row that the ChkDate cell is currently on, in the next available column. i'm going to go do this manually for now, since i have a deadline coming up.

Sub CopyDataUsingDateRange()

Application.ScreenUpdating = False

Dim sd As Date
Dim ed As Date

Dim asd As Date
Dim ChkDate As Range '<----Range
Dim Cell_Range As Range
Dim Cell As Range
Dim DateRange As Range
Dim LastRow As Long
Dim LastName As Long
Dim NameRange As Range
Dim EmptyColumn As Long


'On Error GoTo FML

LastName = Cells(Rows.Count, "B").End(xlUp).Row
LastRow = Cells(Rows.Count, 3).End(xlUp).Row
DateRow = Cells(Rows.Count, "G").End(xlUp).Row
Set DateRange = ThisWorkbook.Worksheets("Agent List").Range("G2:G" & DateRow)
Set Cell_Range = ThisWorkbook.Worksheets("Trial").Range("c2:C" & LastRow)
Set NameRange = ThisWorkbook.Worksheets("Agent List").Range("B2:B" & LastName)
EmptyColumn = Cells(, Columns.Count).End(xlToLeft).Column
Dim X as String

With ThisWorkbook.Worksheets("Agent List")
Do While Not NameRange Is Nothing
sd = 0
ed = 0

For Each Cell In DateRange
If IsDate(Cell) Then
sd = Cell.Value
ed = Cell.Offset(0, 1).Value
X = Cell.Offset(0, -5).Value
For Each ChkDate In Cell_Range
asd = ChkDate.Value
If sd <= asd And ed >= asd Then
'INSERT SOME KIND OF PASTE THAT ACTUALLY WORKS!!!
End If
'Application.CutCopyMode = True
Next ChkDate
End If
Next Cell
Loop
'End If
'Next cell
End With
Exit Sub
FML:
'MsgBox Err.Description

Application.ScreenUpdating = True
End Sub
 
Upvote 0
SOLVED IT!
i did not stay at work until 11pm until i figured this out... :D
it's not perfect and still needs some refinement. But its working! it let me do everything i needed and after i was done writing it it took me less than an hour to finish up :D. i truly hope this helps somebody else :D

VBA Code:
Option Explicit
'Option Explicit is very helpful because it will throw an error on compile if
'you have undefined variables floating around in your project.

Sub LoopRangeFifth() ' i made 5 buttons, so this one was for the 5th. each button has different offsets
Application.ScreenUpdating = False 'Faster runtime

'On Error Resume Next

Dim iWS As Worksheet: Set iWS = ThisWorkbook.Sheets("First")
Dim oWS As Worksheet: Set oWS = ThisWorkbook.Sheets("List")
'output is the worksheet that has data you want to pull from, input is the sheet you're going to be 
'putting data into. i know.. leave me alone, it works.
Dim inputRange As Range: Set inputRange = iWS.Range("c2", iWS.Range("c2").End(xlDown))
Dim outputRange As Range: Set outputRange = oWS.Range("b2", oWS.Range("b2").End(xlDown))
Dim MyCell As Range

Dim startDate As Date
Dim endDate As Date
            
Dim iCell As Range, oCell As Range
Dim i As Integer
i = 1
'icell is going to be for iWS, i is going to be to move cells
For Each oCell In outputRange 'for each name in the list
If Not IsEmpty(oCell) Then 
    If IsDate(oCell.Offset(0, 14)) Then ' if the start date across from the name is a date (not a empty cell)
        startDate = oCell.Offset(0, 14).Value 'set that offset as start date
            endDate = oCell.Offset(0, 15).Value 
        For Each iCell In inputRange 'nested loop. with that start date, start comparing against the new list
            If iCell.Value >= startDate And iCell.Value <= endDate Then  'if compared date is in range
            oCell.Copy 'copy the name of the person with those dates
            iCell.Select 'get activecell, for some reason, paste special errored after 2 ocells
            
Line41:     If IsEmpty(ActiveCell.Offset(0, 1 + i)) Then   'so now for the pasting, offset from active cell (to the right, and add a counter
            GoTo FinishHim 'pasting option
            
            Else
            GoTo ThisisBull 'increase integer option
            End If
            Else
            GoTo Line47
            End If
Line47:     Next iCell
    Else
    GoTo Line49
    End If
Else: GoTo Line49
End If
Line49: Next oCell

Exit Sub
FinishHim:
            ActiveCell.Offset(0, 1 + i).PasteSpecial xlValues, Transpose:=False 'paste in that offset, reset iteger, go to next date to compare
            i = 1
            GoTo Line47
ThisisBull:
    i = i + 1 ' increase integer, check if new offset is empty
    GoTo Line41
' i still need to add error catching, see if i can't speed this up any, and put in some safeguards, but it worked!        
Application.ScreenUpdating = True 
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,431
Messages
6,119,458
Members
448,899
Latest member
maplemeadows

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