Create dynamic table in apointment body

jag108

Active Member
Joined
May 14, 2002
Messages
433
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi Team Mr Excel!

I have an interesting situation that I am trying to find a resolution for, I really hope it can be done using VBA."

Anyway here goes. I have a sheet with a list of servers, these servers are auto patched based on Active Directory groups.

With the help From the Mr Excel experts, I have managed to calculate the next date a server(s) will be patched. Not all servers are patched on the same day, but then again there may be more than one server patched on the same day.

APG_CLAS.xlsm
ABCDEFG
1HostPatching Group (Rule)ScheduleDayOccuranceCurrent DateNext Due Date
2Server1APG-DevTest-ThirdFri-9amTo11amThird Fri5314/07/202017/07/2020
3Server2APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
4Server3APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
5Server4APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
6Server5APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
7Server6APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
8Server7APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
9Server8APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
10Server9APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
11Server10APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
12Server11APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
13Server12APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
14Server13APG-DevTest-ThirdSun-2amTo4amThird Sun7314/07/202019/07/2020
15Server14APG-Prod-FirstSun-2amTo4amFirst Sun7114/07/202002/08/2020
16Server15APG-Prod-FirstSun-2amTo4amFirst Sun7114/07/202002/08/2020
17Server16APG-Prod-FirstSun-2amTo4amFirst Sun7114/07/202002/08/2020
18Server17APG-Prod-FirstSun-2amTo4amFirst Sun7114/07/202002/08/2020
19Server18APG-Prod-FirstSun-2amTo4amFirst Sun7114/07/202002/08/2020
20Server19APG-Prod-FirstWed-2amTo3amFirst Wed3114/07/202005/08/2020
21Server20APG-Prod-FirstWed-2amTo3amFirst Wed3114/07/202005/08/2020
22Server21APG-Prod-FirstWed-2amTo3amFirst Wed3114/07/202005/08/2020
23Server22APG-Prod-FourthSun-0amTo2amFourth Sun7414/07/202026/07/2020
24Server23APG-Prod-FourthSun-0amTo2amFourth Sun7414/07/202026/07/2020
25Server24APG-Prod-FourthSun-0amTo2amFourth Sun7414/07/202026/07/2020
26Server25APG-Prod-FourthSun-0amTo2amFourth Sun7414/07/202026/07/2020
27Server26APG-Prod-FourthSun-0amTo2amFourth Sun7414/07/202026/07/2020
28Server27APG-Prod-FourthSun-0amTo2amFourth Sun7414/07/202026/07/2020
29Server28APG-Prod-FourthSun-2amTo4amFourth Sun7414/07/202026/07/2020
30Server29APG-Prod-FourthSun-2amTo4amFourth Sun7414/07/202026/07/2020
Sheet2
Cell Formulas
RangeFormula
F23:F27,F2:F14F2=NOW()
G2:G30G2=INT((EOMONTH(F2,--(INT((F2-DAY(F2)+7-MOD(D2+1,7))/7)*7+MOD(D2+1,7)+(E2-1)*7<F2)-1)+7-MOD(D2+1,7))/7)*7+MOD(D2+1,7)+(E2-1)*7
F28:F30,F15:F22F15=TODAY()


Row 1 has a single server that is going to be patched on the 17th, this is fine, I can handle this.
How ever when we get to the situation whereby there are multiple servers to be patched on the 19th, I would ideally like to have the server names added into a list\table to be populated in the meeting request body.

I really hope this can be done?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,

I modified my email code from here, which created a single mail for Outlook for rows with same email addresses.

Roughly
The macro looks for and deletes an AppBody sheet.
The macro copies the column header to AppBody
The macro loops down column G to get the first date.
Copies the row to AppBody
A second nested loop checks for the same date
Any rows with matching date are copied
A yes is appended to H on all copied rows so they are not processed again
AppBody content is auto fitted otherwise the date is just #######

The AppBody content is turned into Table 1 so it can be copied to the appointment using Word

The table is then unlisted and the content cleared

The Outlook appointment Code isn't something I use so you'll likely need to sort out the dates for that from cell.value and any other content missed.
I've just used something used before to check the table is copying based on next due date.
Happy to look if you post back.

If you don't want to send the whole rows take a shot at adjusting the code


Code:
Sub Make_App()


Dim AppBody As Range

Set mWs = Worksheets("Sheet2")

'If AppBody sheet already exists then delete it
If WorksheetExists("AppBody") Then
Application.DisplayAlerts = False
Worksheets("AppBody").Delete
Application.DisplayAlerts = True
End If

'Add a sheet to copy all same person rows to
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "AppBody"

'Copy the header
   mWs.Rows(1).Copy Destination:=Worksheets("AppBody").Range("A1")

'Return to the appointment content sheet
   mWs.Activate

'Set due next date as range for first loop to run down
    Set rng = Range(Range("G2"), Range("G" & Rows.Count).End(xlUp))

'Get a row count to clear column H at the end
  i = rng.Rows.Count


For Each cell In rng
If cell.Value <> "" Then
If Not cell.Offset(0, 1).Value = "yes" Then

'Get the row number
    cRow = cell.Row

'Copy the row to the first empty row in the AppBody sheet
mWs.Range("A" & cRow, "G" & cRow).Copy Destination:=Sheets("AppBody").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
 

'Second loop checks the dates of all cells following the current cell in the first loop.
'Yes will be appended on any duplicate finds and another row added to the AppBody sheet
    For Each dwn In rng.Offset(cRow - 1, 0)

    dRow = dwn.Row
    If dwn.Value = cell.Value Then

    dwn.Offset(0, 1).Value = "yes"

'Create additional table row for each extra row found
    mWs.Range("A" & dRow, "G" & dRow).Copy Destination:=Sheets("AppBody").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

    End If

    Next

'Autofit the copied rows on the new sheet, otherwise they'll be copied to the mail with defaults widths, heights
With Worksheets("AppBody")
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set AppBody = .Range(.Cells(1, 1), .Cells(lRow, 7))
.Range("A1:G2").Columns.AutoFit
    End With


'Construct appointment
Set myOutlook = CreateObject("Outlook.Application")
'Set ID = "Identity"
Set MyApt = myOutlook.createitem(1)
MyApt.Subject = "Subject"
MyApt.Start = " 6:00:00 PM"
    MyApt.End = " 7:00:00 PM"

'Activate the Added sheet and turn the range into a table
    Worksheets("AppBody").Activate

    With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.ListObjects.Add(xlSrcRange, Range("$A$1:$G$" & LastRow), , xlYes).Name = _
"Table1"
.ListObjects("Table1").Range.Copy
    End With

'Paste the table into the appointment
MyApt.Display
    MyApt.GetInspector.WordEditor.Windows(1).Selection.PasteExcelTable False, False, False

MyApt.Save





cell.Offset(0, 1).Value = "yes"

'Clear the AppBody rows up to the header
With Worksheets("AppBody")
.ListObjects("Table1").Unlist
.Range("A2:G" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
End With

End If
End If

Next


'Delete AppBody sheet
Application.DisplayAlerts = False
Worksheets("AppBody").Delete
Application.DisplayAlerts = True

'Return to the appointment content sheet
mWs.Activate
'Clear 'yes' from column H
Range("H2:H" & i + 1).ClearContents

End Sub

'Does the worksheet exists
Function WorksheetExists(WSName) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
    End Function
 
Last edited:
Upvote 0
Hi,

I modified my email code from here, which created a single mail for Outlook for rows with same email addresses.

Roughly
The macro looks for and deletes an AppBody sheet.
The macro copies the column header to AppBody
The macro loops down column G to get the first date.
Copies the row to AppBody
A second nested loop checks for the same date
Any rows with matching date are copied
A yes is appended to H on all copied rows so they are not processed again
AppBody content is auto fitted otherwise the date is just #######

The AppBody content is turned into Table 1 so it can be copied to the appointment using Word

The table is then unlisted and the content cleared

The Outlook appointment Code isn't something I use so you'll likely need to sort out the dates for that from cell.value and any other content missed.
I've just used something used before to check the table is copying based on next due date.
Happy to look if you post back.

If you don't want to send the whole rows take a shot at adjusting the code


Code:
Sub Make_App()


Dim AppBody As Range

Set mWs = Worksheets("Sheet2")

'If AppBody sheet already exists then delete it
If WorksheetExists("AppBody") Then
Application.DisplayAlerts = False
Worksheets("AppBody").Delete
Application.DisplayAlerts = True
End If

'Add a sheet to copy all same person rows to
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "AppBody"

'Copy the header
   mWs.Rows(1).Copy Destination:=Worksheets("AppBody").Range("A1")

'Return to the appointment content sheet
   mWs.Activate

'Set due next date as range for first loop to run down
    Set rng = Range(Range("G2"), Range("G" & Rows.Count).End(xlUp))

'Get a row count to clear column H at the end
  i = rng.Rows.Count


For Each cell In rng
If cell.Value <> "" Then
If Not cell.Offset(0, 1).Value = "yes" Then

'Get the row number
    cRow = cell.Row

'Copy the row to the first empty row in the AppBody sheet
mWs.Range("A" & cRow, "G" & cRow).Copy Destination:=Sheets("AppBody").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)


'Second loop checks the dates of all cells following the current cell in the first loop.
'Yes will be appended on any duplicate finds and another row added to the AppBody sheet
    For Each dwn In rng.Offset(cRow - 1, 0)

    dRow = dwn.Row
    If dwn.Value = cell.Value Then

    dwn.Offset(0, 1).Value = "yes"

'Create additional table row for each extra row found
    mWs.Range("A" & dRow, "G" & dRow).Copy Destination:=Sheets("AppBody").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

    End If

    Next

'Autofit the copied rows on the new sheet, otherwise they'll be copied to the mail with defaults widths, heights
With Worksheets("AppBody")
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set AppBody = .Range(.Cells(1, 1), .Cells(lRow, 7))
.Range("A1:G2").Columns.AutoFit
    End With


'Construct appointment
Set myOutlook = CreateObject("Outlook.Application")
'Set ID = "Identity"
Set MyApt = myOutlook.createitem(1)
MyApt.Subject = "Subject"
MyApt.Start = " 6:00:00 PM"
    MyApt.End = " 7:00:00 PM"

'Activate the Added sheet and turn the range into a table
    Worksheets("AppBody").Activate

    With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.ListObjects.Add(xlSrcRange, Range("$A$1:$G$" & LastRow), , xlYes).Name = _
"Table1"
.ListObjects("Table1").Range.Copy
    End With

'Paste the table into the appointment
MyApt.Display
    MyApt.GetInspector.WordEditor.Windows(1).Selection.PasteExcelTable False, False, False

MyApt.Save





cell.Offset(0, 1).Value = "yes"

'Clear the AppBody rows up to the header
With Worksheets("AppBody")
.ListObjects("Table1").Unlist
.Range("A2:G" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
End With

End If
End If

Next


'Delete AppBody sheet
Application.DisplayAlerts = False
Worksheets("AppBody").Delete
Application.DisplayAlerts = True

'Return to the appointment content sheet
mWs.Activate
'Clear 'yes' from column H
Range("H2:H" & i + 1).ClearContents

End Sub

'Does the worksheet exists
Function WorksheetExists(WSName) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
    End Function

Awesome, thanks Dave, just what I was after.
 
Upvote 0
Just noticed:
Dim AppBody As Range ??????
Previous email code reference.

AppBody is a worksheet now.
 
Upvote 0
Just noticed:
Dim AppBody As Range ??????
Previous email code reference.

AppBody is a worksheet now.

Thanks, figured that one out. Just adding some extra bits and pieces now like attendees, location etc...
The code you gave would have taken me a month of Sundays to figure out, so thanks very much.
 
Upvote 0

Forum statistics

Threads
1,223,639
Messages
6,173,499
Members
452,517
Latest member
SoerenB

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