Macro to take data from a grid and add it to my calender under that persons name?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyne,

I'll try explain this the best I can hopefully its clear.

Firstly let me say Not all cells n range will have data in them!

so, I have A Sheet Called "Calendar"

In this Sheet Column H is employees names, (Start at Row 11)
Row 10 is my dates in columns M:AHM
just standard dates from 1st Jan on (01/01/2018)

Now I have another sheet that hold the data I need to put into this calendar.
that sheet is Data3

So BB holds the start Date
BC Holds the End Date for each Job.

Then BE to BX holds the names of the employees doing this Job
There might be only one employee, there might 20 max

so I need a macro that can go Down column BB starting in row 6, if its empty then goto next row when you find a row that is not empty, that's your start date, look in same row BC and that's the end date.
then look along that row from column BE and take the names in any cells from BE to BX these are the employee names.
so each employee has their own row in
Sheet Called "Calendar"
Column H, so find that employee find the startdate in row 10 and ADD the letters "OUT" to Every Cell form start date to end date.

hope that's clear

heres a very small example:


[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD]BB
[/TD]
[TD]BC
[/TD]
[TD][/TD]
[TD]BE
[/TD]
[TD]BC
[/TD]
[TD]BD
[/TD]
[TD]BE
[/TD]
[TD]BF
[/TD]
[TD]BG
[/TD]
[TD]BH
[/TD]
[TD]BI[/TD]
[TD]to
[/TD]
[TD]BX
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD][/TD]
[TD]start date
[/TD]
[TD]end date
[/TD]
[TD][/TD]
[TD]employees names
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]
[/TD]
[TD]12/05/2018
[/TD]
[TD]14/05/2018
[/TD]
[TD]
[/TD]
[TD]Tony
[/TD]
[TD]Bob
[/TD]
[TD]Sara
[/TD]
[TD]Jon
[/TD]
[TD][/TD]
[TD]so example 1 Tony, bob,sara and jon need "OUT" for this date
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD]
[/TD]
[TD]9/5/18
[/TD]
[TD]10/5/18
[/TD]
[TD]
[/TD]
[TD]simon
[/TD]
[TD]Tony
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]but this time its just two names
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]10
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]some gaps will appear,
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]11
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]You can Find last row using Column G as that has data in every row!
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]12
[/TD]
[TD]
[/TD]
[TD]4/5/18
[/TD]
[TD]7/5/18
[/TD]
[TD]
[/TD]
[TD]BOB
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]13
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]14
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD]H
[/TD]
[TD][/TD]
[TD]M
[/TD]
[TD]n
[/TD]
[TD]o
[/TD]
[TD]p
[/TD]
[TD]q
[/TD]
[TD]r
[/TD]
[TD]s
[/TD]
[TD]t
[/TD]
[TD]u
[/TD]
[TD]v
[/TD]
[TD]w
[/TD]
[TD]x
[/TD]
[TD]y
[/TD]
[TD]z
[/TD]
[TD]aa
[/TD]
[TD]ab
[/TD]
[TD]ac
[/TD]
[TD]ad
[/TD]
[TD]ae
[/TD]
[TD]af
[/TD]
[TD]ag
[/TD]
[/TR]
[TR]
[TD]10
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1/5/18
[/TD]
[TD]2/5/18
[/TD]
[TD]3/5/18
[/TD]
[TD]4/5/18
[/TD]
[TD]5/5/18
[/TD]
[TD]6/5/18
[/TD]
[TD]7/5/18
[/TD]
[TD]8/5/18
[/TD]
[TD]9/5/18
[/TD]
[TD]10/5/18
[/TD]
[TD]11/5/18
[/TD]
[TD]12/5/18
[/TD]
[TD]13/5/18
[/TD]
[TD]14/5/18
[/TD]
[TD]15/5/18
[/TD]
[TD]16/5/18
[/TD]
[TD]17/5/18
[/TD]
[TD]DATES CONTINUE one day at a time until column AHM
[/TD]
[TD][/TD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TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

So please help if you can.

Thanks

Tony
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this:
Activate Microsoft Scripting Runtime from Tools-->References
Create a Class Module - name it clsCalendar and write following code in it:

Option Explicit


Public Person As String
Public StartDate As Date
Public EndDate As Date


Then in your module - paste the following:

Option Explicit
Global Datasheet As Worksheet
Global CalendarSheet As Worksheet






Sub Main()


Set Datasheet = ThisWorkbook.Worksheets("Data3")
Set CalendarSheet = ThisWorkbook.Worksheets("Calendar")
Dim dict As Dictionary

Set dict = ReadData

PrintCalendar dict





End Sub




Function ReadData()

Dim DataRange As Range, cell As Variant
Dim FirstRow As Long, lastrow As Long, RowCounter As Long, ColumnCounter As Long
Dim dict As New Dictionary
dict.CompareMode = TextCompare
Dim oCalendar As clsCalendar

FirstRow = 6
lastrow = Datasheet.Cells(Rows.Count, 54).End(xlUp).Row
Dim Person As String
Dim CalendarID As Long


For RowCounter = FirstRow To lastrow

For ColumnCounter = 57 To 76
Person = Datasheet.Cells(RowCounter, ColumnCounter)
If Person = "" Then GoTo NextRow
CalendarID = CalendarID + 1
Set oCalendar = New clsCalendar
With oCalendar
.Person = Person
.StartDate = Datasheet.Cells(RowCounter, "BB")
.EndDate = Datasheet.Cells(RowCounter, "BC")
End With

dict.Add CalendarID, oCalendar

Next ColumnCounter
NextRow:
Next RowCounter


Set ReadData = dict

End Function


Sub PrintCalendar(dict As Dictionary)


Dim oCalendar As clsCalendar
Dim key As Variant
Dim Name As String, StartDate As Date, EndDate As Date
Dim Off As Long

Dim calendarStart As Long, calendarEnd As Long
calendarStart = 13 'Column M
calendarEnd = 897 'Column AHM

Dim EntryStart As Long, EntryEnd As Long
Dim ColumnCounter As Long

'Clear existing content
Dim rg As Range
Set rg = CalendarSheet.Range("M11").CurrentRegion
rg.Offset(1).Clear

For Each key In dict.Keys
With dict(key)
Name = .Person
StartDate = .StartDate
EndDate = .EndDate
End With

Off = GetOffset(Name)

For ColumnCounter = calendarStart To calendarEnd
If CalendarSheet.Cells(11, ColumnCounter).Value >= StartDate And _
CalendarSheet.Cells(11, ColumnCounter).Value <= EndDate Then

CalendarSheet.Cells(11, ColumnCounter).Offset(Off).Value = "OUT"

End If

Next ColumnCounter


Next key


End Sub


Function GetOffset(Name As String)


Dim HeaderRow As Long
HeaderRow = 11

Dim lastrow As Long
lastrow = CalendarSheet.Cells(Rows.Count, "H").End(xlUp).Row

Dim OffsetNumber As Long, N As String

For OffsetNumber = 1 To 100000
N = CalendarSheet.Cells(HeaderRow + OffsetNumber, "H").Value
If UCase(N) = UCase(Name) Then GoTo EndFunction
Next OffsetNumber
EndFunction:

GetOffset = OffsetNumber



End Function


Works for me in small scale :-)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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